Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compressing UIImage into JPEG image data multiple times

I am debugging a piece of code where an UIImage may be gone through UIImageJPEGRepresentation multiple times, I thought that must be a bug and the image quality will get worsen, but surprisingly we can't see the difference visually.

So I did a test, loading an image, and try to let it go through UIImageJPEGRepresentation 1000 times, surprisingly, whether 1 or 1000 times doesn't really make a difference in the image quality visually, why is that so?

This is the testing code:

        UIImage *image = [UIImage imageNamed:@"photo.jpeg"];

        // Create a data reference here for the for loop later
        // First JPEG compression here
        // I would imagine the data here already has low image quality
        NSData *data = UIImageJPEGRepresentation(image, 0);

        for(int i=0; i<1000; i++)
        {
            // Convert the data with low image quality to UIImage
            UIImage *image = [UIImage imageWithData:data];

            // Compress the image into a low quality data again
            // at this point i would imagine the image get even more low quality, like u resaved a jpeg twice in phootshop
            data = UIImageJPEGRepresentation(image, 0);
        }

        // up to this point I would imagine the "data" has gone through JPEG compression 1000 times
        // like you resave a jpeg as a jpeg in photoshop 1000 times, it should look like a piece of crap
        UIImage *imageFinal = [UIImage imageWithData:data];
        UIImageView *view = [[UIImageView alloc] initWithImage:imageFinal];
        [self.view addSubview:view];

        // but it didn't, the final image looks like it has only gone through the jpeg compression once.

EDIT: my doubt can be summarised into a simpler code, if you do this in objectiveC:

        UIImage *image1 = an image..
        NSData *data1 = UIImageJPEGRepresentation(image1, 0);
        UIImage *image2 = [UIImage imageWithData:data1];
        NSData *data2 = UIImageJPEGRepresentation(image2, 0);
        UIImage *imageFinal = [UIImage imageWithData:data2];

Did imageFinal gone through JPEG compression twice?

like image 753
mkto Avatar asked Apr 09 '14 05:04

mkto


People also ask

How do I compress a PNG in IOS Swift?

You can't. If you need to compress your data just use jpeg.

How do I reduce the size of an image in Swift?

Create an extension Reduce the image height so it becomes smaller and uses less space. Create a new file called Extensions. swift and import SwiftUI at the top. Then, create an extension for UIImage, called aspectFittedToHeight, which will take a newHeight and return a smaller UIImage.

How do I reduce the size of an image in Objective C?

Hope this is much faster then the accepted answer. NSData *imageData; imageData=[[NSData alloc] initWithData:UIImageJPEGRepresentation((chosenImage), 1.0)]; NSLog(@"[before] image size: %lu--", (unsigned long)[imageData length]); CGFloat scale= (100*1024)/(CGFloat)[imageData length]; // For 100KB.


1 Answers

As you know, JPG compression works by altering the image to produce smaller file size. The reason why you don't see progressively worse quality is because you're using the same compression setting each time.

The algorithm alters the source image just enough to fit into the compression profile - in other words, compressing the result of 50% JPG again at 50% will produce the same image, because the image doesn't need to be altered any more.

You can test this in Photoshop - save a photo out at say 30% quality JPG. Reopen the file you just saved, and go to Save for Web - flip between PNG (uncompressed/original) and JPG 30% - there will be no difference.

Hope this helps.

like image 84
Kirill E. Avatar answered Sep 29 '22 06:09

Kirill E.