2012-03-21 106 views
6

在iOS中将UIImage另存为JPEG,我使用UIImageJPEGRepresentation,但是它不采用压缩比以外的选项。我希望将UIImage保存为progressive JPEG格式,有没有简单的方法可以做到这一点?如何在iOS中将UIImage保存为渐进式JPEG格式?

看起来像在OS X中有一个NSImageProgressive选项将NSImage保存为渐进格式。

+0

你尝试'UIImageJPEGRepresentation(<#的UIImage *图像#>, <#CGFloat compressionQuality#>)' – hchouhan02 2012-03-22 11:54:00

+0

这并不能使它渐进。 – 2012-03-23 06:22:58

回答

7

我想你可以使用的ImageIO框架,这样做:

#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说,输出文件是渐进的,而ImageMagick的identify命令说,它有“隔行扫描:JPEG”。

+0

我已经在iOS设备上测试过,并且是不同形式的模拟器。 – user501836 2012-06-18 07:24:09

+0

结果是一样的http://stackoverflow.com/questions/10829100/creating-progressive-jpeg-on-ios-with-imageio-produces-blocky-results-on-device – user501836 2012-06-18 07:25:21

0

此代码在设备上工作 - 关键是要指定所有的密度值(注意其使用新文本语法):

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

不应该'(__bridge id) kCFBooleanTrue,kCGImagePropertyJFIFIsProgressive'是'jfifProperties'的一部分吗?我不得不补充说,实际上是为了将它保存为渐进式图像。 – qix 2013-08-30 19:54:33

+0

@Linus,当我回答这个问题时,要么我错过了这个,要么没有这样的属性。显然这个答案对任何人都没有用,当然不是原来的海报,所以我会查看这里有什么怀疑(尽管当然我很确定它的正确或我不会发布它:-)) – 2013-08-30 22:56:59

相关问题