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

时间:2022-10-06 21:22:27

In iOS to save an UIImage as JPEG, I use UIImageJPEGRepresentation, however it doesn't take options other than compression ratio. I wish to save the UIImage into progressive JPEG format, is there a easy way to do so?

在iOS中将UIImage保存为JPEG,我使用的是UIImageJPEGRepresentation,但是除了压缩率之外,它不会采用其他选项。我希望将UIImage保存为渐进式JPEG格式,有没有简单的方法呢?

Looks like in OS X there is an NSImageProgressive option to save NSImage to progressive format.

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

2 个解决方案

#1


7  

I think you can do it using the ImageIO framework, like this:

我想你可以使用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 says the output file is progressive, and ImageMagick's identify command says it has “Interlace: JPEG”.

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

#2


0  

This code does work on the device - the key is to specify all the density values (note its using new literal syntax):

此代码适用于设备 - 关键是指定所有密度值(请注意其使用新的文字语法):

- (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];
}

#1


7  

I think you can do it using the ImageIO framework, like this:

我想你可以使用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 says the output file is progressive, and ImageMagick's identify command says it has “Interlace: JPEG”.

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

#2


0  

This code does work on the device - the key is to specify all the density values (note its using new literal syntax):

此代码适用于设备 - 关键是指定所有密度值(请注意其使用新的文字语法):

- (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];
}