vendredi 20 février 2015

iOS - How to best resize image and determine size in bytes?

I'm taking a square-cropped image from UIImagePickerController on an iPhone and I want to downsize it to thumbnail size 90x90. I need to be able to break up the image in data chunks to transfer, so I want to balance quality and size.


My first question is: why is the final data length in bytes 9,200 not equal to the length before the image is recreated from the same bytes 3,600.


Additionally, what is the actual size of the final image? Is there a better way to go about resizing? I'm probably missing some concepts here, but above all, I'd like to understand what's going on per my first question. See code with comments. Thanks!



-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

NSData *imageData;
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];

imageData = UIImageJPEGRepresentation(image, 1);
// imageData is about 408,000 bytes.

UIGraphicsBeginImageContextWithOptions(CGSizeMake(90, 90), YES, 1.0);
[image drawInRect:CGRectMake(0, 0, 90, 90)];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

imageData = UIImageJPEGRepresentation(image, 1);
// imageData is about 12,800 bytes.

imageData = UIImageJPEGRepresentation(image, .7);
// imageData is about 3,600 bytes.

image = [UIImage imageWithData:imageData];

imageData = UIImageJPEGRepresentation(image, 1);
// imageData is about 9,200 bytes.

[self dismissViewControllerAnimated:YES completion:nil];
}

Aucun commentaire:

Enregistrer un commentaire