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?
Looks like in OS X there is an NSImageProgressive
option to save NSImage
to progressive format.
In the “File” menu, select “Export” and choose the option “Save for web (Legacy)” On the upper right, choose JPEG as the file format, and check “Progressive” Set the image quality as needed. Click “Save” on the bottom left.
A progressive JPEG image is encoded differently than a standard or baseline JPEG image. It loads in successive waves until a clear picture is formed. This can improve a website's performance as the images seems to be loading faster.
Less waiting time for users With baseline JPEG, your users see a large white space on the screen or a loading circle spinning until the image has finished loading. With progressive JPEG, visitors can already see the entire image at first sight. Although it's blurry, they can still understand what's in the image.
JPEG images are either progressive or nonprogressive, depending on their encoding order, not politics. Encoding of and decoding of nonprogressive occurs in this simple order: from top to bottom and from left to right.
I think you can do it using the ImageIO framework, like this:
#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”.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With