Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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.

like image 908
Robert Mao Avatar asked Mar 21 '12 22:03

Robert Mao


People also ask

How do I save an image as a progressive JPEG?

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.

When saving JPEG What is progressive?

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.

Should I save JPEG as baseline or progressive?

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.

Is JPEG progressive?

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.


1 Answers

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”.

like image 122
rob mayoff Avatar answered Sep 26 '22 12:09

rob mayoff