Foresee UIImageJPEGRepresentation image size before the conversion

1.3k Views Asked by At

When you select an image to email using the iPhone photo library it allows you to select the image size (low, medium, high). My question is: how it's calculating the final image size? I'm trying to do something similar, but if I actually do the conversion using different compression rates (UIImageJPEGRepresentation) the process is very slow. My guess is that Apple is doing some kind of approximation with the original PNG size. Any thoughts?

Example of a slow version:

float size =  [UIImagePNGRepresentation(self.image) length]/1024.0/1024.0;

NSString *high = [NSString stringWithFormat:@"High (%.2f %@)",size > 1 ? size : size * 1024.0,size > 1 ? @"MB" : @"KB"];

size =  [UIImageJPEGRepresentation(self.image,0.9) length]/1024.0/1024.0;

NSString *medium = [NSString stringWithFormat:@"Medium (%.2f %@)",size > 1 ? size : size * 1024.0,size > 1 ? @"MB" : @"KB"];

etc.

0

There are 0 best solutions below