2012-03-21 9 views
6

iOS'ta bir UIImage dosyasını JPEG olarak kaydetmek için UIImageJPEGRepresentation kullanıyorum, ancak sıkıştırma oranı dışındaki seçenekleri almaz. UIImage'u progressive JPEG biçiminde kaydetmeyi diliyorum, bunu yapmanın kolay bir yolu var mı?Bir UIImage, iOS'ta aşamalı JPEG biçimine nasıl kaydedilir?

OS X'de, NSImage'u aşamalı biçimde kaydetmek için NSImageProgressive seçeneği var.

+0

Eğer 'UIImageJPEGRepresentation (<# UIImage * görüntü #> denedin mi, <#CGFloat compressionQuality #>) – hchouhan02

+0

Bu, aşamalı hale getirmiyor. –

cevap

7

Ben böyle, ImageIO çerçevesini kullanarak bunu yapabilirsiniz düşünüyorum:

#import <UIKit/UIKit.h> 
#import <ImageIO/ImageIO.h> 
#import <MobileCoreServices/MobileCoreServices.h> 
#import "AppDelegate.h" 

int main(int argc, char *argv[]) 
{ 
    @autoreleasepool { 
     UIImage *sourceImage = [UIImage imageNamed:@"test.jpg"]; 

     CFURLRef url = CFURLCreateWithString(NULL, CFSTR("file:///tmp/progressive.jpg"), NULL); 
     CGImageDestinationRef destination = CGImageDestinationCreateWithURL(url, kUTTypeJPEG, 1, NULL); 
     CFRelease(url); 

     NSDictionary *jfifProperties = [NSDictionary dictionaryWithObjectsAndKeys: 
      (__bridge id)kCFBooleanTrue, kCGImagePropertyJFIFIsProgressive, 
      nil]; 

     NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys: 
      [NSNumber numberWithFloat:.6], kCGImageDestinationLossyCompressionQuality, 
      jfifProperties, kCGImagePropertyJFIFDictionary, 
      nil]; 

     CGImageDestinationAddImage(destination, sourceImage.CGImage, (__bridge CFDictionaryRef)properties); 
     CGImageDestinationFinalize(destination); 
     CFRelease(destination); 

     return 0; 
    } 
} 

Preview.app çıktı dosyası ilerici olduğunu söylüyor ve ImageMagick en identify komutu “ıç içe: JPEG” olduğunu söylüyor.

+0

iOS cihazında test var ve farklı form simülatörü. – user501836

+0

Sonuç aynıdır: http://stackoverflow.com/questions/10829100/creating-progressive-jpeg-on-ios-with-imageio-produces-blocky-results-on-device – user501836

0

Bu kod cihazda iş yapar - tuş (onun kullanarak yeni değişmez sözdizimini dikkat edin) tüm yoğunluk değerleri belirtmek için ise:

- (UIImage *)imager 
{ 
    UIImage *object = [UIImage imageNamed:@"Untitled.jpg"]; 
    NSString *str = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"Tester.jpg"]; 
    NSURL *url = [[NSURL alloc] initFileURLWithPath:str]; 

    CGImageDestinationRef destination = CGImageDestinationCreateWithURL((__bridge CFURLRef)url, kUTTypeJPEG, 1, NULL); 

    NSDictionary *jfifProperties = [NSDictionary dictionaryWithObjectsAndKeys: 
            @72, kCGImagePropertyJFIFXDensity, 
            @72, kCGImagePropertyJFIFYDensity, 
            @1, kCGImagePropertyJFIFDensityUnit, 
            nil]; 

    NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys: 
           [NSNumber numberWithFloat:.5f], kCGImageDestinationLossyCompressionQuality, 
           jfifProperties, kCGImagePropertyJFIFDictionary, 
           nil]; 

    CGImageDestinationAddImage(destination, ((UIImage*)object).CGImage, (__bridge CFDictionaryRef)properties); 
    CGImageDestinationFinalize(destination); 
    CFRelease(destination); 

    return [UIImage imageWithContentsOfFile:str]; 
} 
+0

Shouldn't '(__bridge id) kCFBooleanTrue, kCGImagePropertyJFIFIsProgressive 'jfifProperties' bir parçası olmak? Bunu, ilerici bir görüntü olarak kaydetmek için onu almam gerekiyordu. – qix

+0

@ Linus, bu soruya cevap verdiğimde ya bunu özledim ya da böyle bir özellik yoktu. Açıkçası bu cevap, hiç kimseye, hiç şüphesiz, orijinal posteri kullanmamıştı, bu yüzden şüphe ile burada olanları görebiliyordum (tabi ki doğru olduğuna eminim ya da yayınlamamıştım :-)) –