1

so what im doing is compressing an image repeatedly until it fits within a size frame. It it takes to many attempts, give up and later we tell the user the image is to large.

My issue is that after the image size has been reduced, its size goes WAY back up again when its changed to a UIImage.

Here is the code for reducing the image size:

    double compressionRatio = 1;
    int resizeAttempts = 5;

    NSData * imgData = UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"],compressionRatio);

    NSLog(@"Starting Size: %i", [imgData length]);

    //Trying to push it below around about 0.4 meg
    while ([imgData length] > 400000 && resizeAttempts > 0) {
        resizeAttempts -= 1;

        NSLog(@"Image was bigger than 400000 Bytes. Resizing.");
        NSLog(@"%i Attempts Remaining",resizeAttempts);

        //Increase the compression amount
        compressionRatio = compressionRatio*0.5;

        //Test size before compression
        NSLog(@"Current Size: %i",[imgData length]);
        imgData = UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"],compressionRatio);

        //Test size after compression
        NSLog(@"New Size: %i",[imgData length]);
    }

    //Set image by comprssed version
    savedImage = [UIImage imageWithData:imgData];

    //Check how big the image is now its been compressed and put into the UIImageView
    NSData *endData = UIImageJPEGRepresentation(savedImage,1.0);
    NSLog(@"Ending Size: %i", [endData length]);

Ok now here is the console report:

Starting Image Size: 4076994

Image was bigger than 400000 Bytes. Resizing.
4 Attempts Remaining
Current Size: 4076994
New Compressed Size: 844482

Image was bigger than 400000 Bytes. Resizing.
3 Attempts Remaining
Current Size: 844482
New Compressed Size: 357459

Ending Image Size: 2090332

As you can see the image started at 4mb big. It was compressed to 350kb, then in the end it was made into a UIImage as 2mb.

Thanks in advance to anyone who can make any sense of this.

Dipen Panchasara
  • 13,480
  • 5
  • 47
  • 57
Necro
  • 333
  • 5
  • 18
  • 1
    You are compressing the image, then expanding it to a UIImage again, then printing the uncompressed size (1.0 in the last function). Explanations on how to do this [already exist](http://stackoverflow.com/questions/9506871/image-compression-by-size-iphone-sdk) – Paul de Lange May 13 '13 at 09:37

1 Answers1

1

I have modified your function a bit and got result, try it, it reduce my 10MB image to 3MB.

-(void)compraseImage
{
    UIImage *largeImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

    double compressionRatio = 1;
    int resizeAttempts = 5;

    NSData * imgData = UIImageJPEGRepresentation(largeImage,compressionRatio);

    NSLog(@"Starting Size: %i", [imgData length]);

    //Trying to push it below around about 0.4 meg
    while ([imgData length] > 400000 && resizeAttempts > 0) {
        resizeAttempts -= 1;

        NSLog(@"Image was bigger than 400000 Bytes. Resizing.");
        NSLog(@"%i Attempts Remaining",resizeAttempts);

        //Increase the compression amount
        compressionRatio = compressionRatio*0.5;
        NSLog(@"compressionRatio %f",compressionRatio);
        //Test size before compression
        NSLog(@"Current Size: %i",[imgData length]);
        imgData = UIImageJPEGRepresentation(largeImage,compressionRatio);

        //Test size after compression
        NSLog(@"New Size: %i",[imgData length]);
    }

    //Set image by comprssed version
    savedImage = [UIImage imageWithData:imgData];

    //Check how big the image is now its been compressed and put into the UIImageView

    // *** I made Change here, you were again storing it with Highest Resolution ***
    NSData *endData = UIImageJPEGRepresentation(largeImage,compressionRatio);
    NSLog(@"Ending Size: %i", [endData length]);

    NSString *path = [self createPath:@"myImage.jpg"];
    NSLog(@"%@",path);
    [endData writeToFile:path atomically:YES];
}

-(NSString *) createPath:(NSString *)withFileName
{
    NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,
                                                        YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:withFileName];
    return path;
}
Dipen Panchasara
  • 13,480
  • 5
  • 47
  • 57
  • Im going to test your changes, but explain this. Why after compression is it 2mb im my case instead of 4mb if thats how big it was originally? – Necro May 13 '13 at 09:54
  • second parameter in `UIImageJPEGRepresentation` indicates the `compressionQuality` which you are calculating in loop reducing compressionRatio until it reach <4M or max 5 iteration. at the last iteration you will get the compression ration of your image, and save that image with final compression ration thats it. if you again save it with maximum quality it will increase the size of image. and your 4MB image would be compressed around 600kb-800kb depending on your image – Dipen Panchasara May 13 '13 at 09:57
  • Oh, i misunderstood what was happening. My userstanding was that i had saved my image with the new compressionQuality. Then the image could be converted back to NSData with no compressionQuality setting because the compressionQuality had allready previously been set. And such when the NSData length is checked it would show the the equal langth as when we were going though the while loop. – Necro May 13 '13 at 10:03
  • excately, you got it perfect. – Dipen Panchasara May 13 '13 at 10:07