如何在iOS中将UIImage保存为渐进式JPEG格式?

Rob*_*Mao 7 iphone jpeg objective-c uiimage ios

在iOS中保存UIImage为JPEG,我使用UIImageJPEGRepresentation,但它不采取压缩率以外的选项.我希望保存UIImageprogressive JPEG格式,有一个简单的方法吗?

在OS X中看起来有一个NSImageProgressive选项可以保存NSImage为渐进格式.

rob*_*off 8

我认为你可以使用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;
    }
}
Run Code Online (Sandbox Code Playgroud)

Preview.app表示输出文件是渐进式的,ImageMagick的identify命令说它有"Interlace:JPEG".