[英]How to save a UIImage into progressive JPEG format in iOS?
In iOS to save an UIImage
as JPEG, I use UIImageJPEGRepresentation
, however it doesn't take options other than compression ratio. 在iOS
UIImageJPEGRepresentation
UIImage
保存为JPEG,我使用UIImageJPEGRepresentation
,但是除了压缩率UIImageJPEGRepresentation
,它不采用其他选项。 I wish to save the UIImage
into progressive JPEG
format, is there a easy way to do so? 我希望将
UIImage
保存为progressive JPEG
格式,有没有简单的方法呢?
Looks like in OS X there is an NSImageProgressive
option to save NSImage
to progressive format. 在OS X中看起来有一个
NSImageProgressive
选项可以将NSImage
保存为渐进格式。
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”。
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];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.