How to save UIImage in progressive JPEG in iOS?

In iOS, I use UIImageJPEGRepresentation to save UIImage in JPEG format, however it does not accept parameters other than compression ratio. I want to save the UIImage format in progressive JPEG , is there an easy way to do this?

It looks like OS X has the NSImageProgressive option to save NSImage in a progressive format.

+6
source share
2 answers

I think you can do this using the ImageIO environment, for example:

 #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 the ImageMagick identify team says it has "interlaced: JPEG."

+7
source

This code works on the device - the key must indicate all density values ​​(note the use of the 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]; } 
0
source

Source: https://habr.com/ru/post/911366/


All Articles