Compress UIImage to JPEG Image Data Multiple Times

I am debugging a piece of code in which a UIImage can go through a UIImageJPEGR presentation several times, I thought that it should be a mistake, and the image quality will deteriorate, but it is surprising that we cannot see the difference visually.

So, I ran the test, uploaded the image and tried to let it go through the UIImageJPEGR presentation 1000 times, which is surprising: 1 or 1000 times doesn’t really affect the image quality visually, why is this?

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 generalized to simpler code if you do this in the C lens:

        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 happen twice through JPEG compression?

+4
3

, JPG . , , , .

, , - , 50% JPG 50% , .

Photoshop - , , 30% JPG. , , " " - PNG (/) JPG 30% - .

, .

+2

. , , :

: , . , , .

: , . .

, .

+1

. , ;

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

for(int i=100; i>0; i--)
{
    UIImage *image = [UIImage imageWithData:data];
    NSData *data = UIImageJPEGRepresentation(image, (0.1 * i);
    NSLog(@"%d",data.length);
}
0

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


All Articles