4

What's the best practice to optimize the weight of an image before uploading it on the server in iOS?

The image can from the images library of the user or directly for the UIPicker - camera mode.

I do have some requirements : a minimum upload resolution and a wished maximum upload size.

Let 's say kMaxUploadSize = 50 kB and kMinUploadResolution = 1136 * 640

What I currently do is :

while (UIImageJPEGRepresentation(img,1.0).length > MAX_UPLOAD_SIZE){
    img = [self scaleDown:img withFactor:0.1];
}
NSData *imageData = UIImageJPEGRepresentation(img,1.0);

-(UIImage*)scaleDown:(UIImage*)img withFactor:(float)f{

CGSize newSize = CGSizeMake(img.size.width*f, img.size.height*f);

UIGraphicsBeginImageContextWithOptions(newSize, YES, 0.0);
[img drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return scaledImage;

}

The time spend in each loop is awfully, several seconds, which leads to a very long delay before effectively sending the image to the server.

Any approach / idea / strategy ?

Thanks a lot !

Romain Dorange
  • 831
  • 1
  • 10
  • 23
  • You can do like this http://stackoverflow.com/questions/2079543/how-do-i-reduce-image-quality-size-in-iphone-objective-c – rakeshNS Aug 23 '13 at 09:16
  • How about this SO answer: [http://stackoverflow.com/questions/9506871/image-compression-by-size-iphone-sdk][1] [1]: http://stackoverflow.com/questions/9506871/image-compression-by-size-iphone-sdk – Uygar Y Aug 23 '13 at 09:20
  • @rakeshNS That's exactly what he is doing... – Mundi Aug 23 '13 at 10:18

1 Answers1

11

thanks your for your feedback. Here is what I decided to do and is great in terms of performances : resizing to the wished resolution, then and only then do iterative compression until reachning the wished size.

Some sample code :

//Resize the image 
float factor;
float resol = img.size.height*img.size.width;
if (resol >MIN_UPLOAD_RESOLUTION){
    factor = sqrt(resol/MIN_UPLOAD_RESOLUTION)*2;
    img = [self scaleDown:img withSize:CGSizeMake(img.size.width/factor, img.size.height/factor)];
}

//Compress the image
CGFloat compression = 0.9f;
CGFloat maxCompression = 0.1f;

NSData *imageData = UIImageJPEGRepresentation(img, compression);

while ([imageData length] > MAX_UPLOAD_SIZE && compression > maxCompression)
{
    compression -= 0.10;
    imageData = UIImageJPEGRepresentation(img, compression);
    NSLog(@"Compress : %d",imageData.length);
}

and

- (UIImage*)scaleDown:(UIImage*)img withSize:(CGSize)newSize{
    UIGraphicsBeginImageContextWithOptions(newSize, YES, 0.0);
    [img drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage; 
}

Thanks

Kousik
  • 21,485
  • 7
  • 36
  • 59
Romain Dorange
  • 831
  • 1
  • 10
  • 23