2012-03-21 21 views
6

Trong iOS để lưu UIImage làm JPEG, tôi sử dụng UIImageJPEGRepresentation, tuy nhiên nó không có các tùy chọn khác ngoài tỷ lệ nén. Tôi muốn lưu UIImage thành định dạng progressive JPEG, có cách nào dễ dàng để làm như vậy không?Làm cách nào để lưu UIImage thành định dạng JPEG lũy tiến trong iOS?

Có vẻ như trong OS X có tùy chọn NSImageProgressive để lưu NSImage thành định dạng lũy ​​tiến.

+0

bạn đã thử 'UIImageJPEGRepresentation (<# UIImage * image #>, <#CGFloat compressionQuality #>) ' – hchouhan02

+0

Điều đó không làm cho nó tiến bộ. –

Trả lời

7

Tôi nghĩ bạn có thể làm điều đó bằng cách sử dụng khuôn khổ ImageIO, như thế này:

#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 nói các tập tin đầu ra là tiến bộ, và lệnh ImageMagick của identify nói nó có “Interlace: JPEG”.

+0

Tôi có thử nghiệm trên thiết bị iOS và là trình mô phỏng biểu mẫu khác nhau. – user501836

+0

Kết quả tương tự với http://stackoverflow.com/questions/10829100/creating-progressive-jpeg-on-ios-with-imageio-produces-blocky-results-on-device – user501836

0

Mã này làm việc trên thiết bị - chính là để chỉ định tất cả các giá trị mật độ (lưu ý sử dụng cú pháp theo nghĩa đen mới của nó):

- (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

Không nên '(__bridge id) kCFBooleanTrue, kCGImagePropertyJFIFIsProgressive' là một phần của 'jfifProperties'? Tôi phải thêm vào đó để thực sự làm cho nó tiết kiệm như một hình ảnh tiến bộ. – qix

+0

@Linus, khi tôi trả lời câu hỏi này hoặc tôi đã bỏ lỡ điều này, hoặc không có tài sản như vậy. Rõ ràng câu trả lời này đã không được sử dụng cho bất cứ ai chắc chắn không phải là poster gốc, vì vậy tôi sẽ xem những gì ở đây với sự nghi ngờ (mặc dù tất nhiên tôi khá chắc chắn nó chính xác hoặc tôi sẽ không đăng nó :-)) –

Các vấn đề liên quan