简体   繁体   中英

iOS returning image with different size?

I want to upload an image up to 8 mb. so for testing I have added image in my photo gallery of size 4.2 mb (dimension : 3264 X 2443). But when I am picking that image for uploading, I have checked size of image. But it returning 9840076 bytes ie 9.3842 mb which is 4.2 mb in actual. So image of size 4.2 mb is not able to upload.

I have used below method to calculate size of image, and this is returning 9840076 bytes.

[UIImageJPEGRepresentation(imageRerurnedFromPhotoGallery , 1.0) length];

Am I doing something wrong in calculating size of image? Please suggest me the proper way. Thanks in advance!

why dont you try like this?

you will get size in bytes

NSData *data = [[NSData alloc] initWithData:UIImageJPEGRepresentation((Your Image), 0.5)];

int imageSize = data.length;
NSLog(@"size of image is: %i ", imageSize);

You are decoding and then re-encoding the image, which is why the file size may be different. If you don't need to modify the image at all, use ALAssetsLibrary to get the image as an NSData object rather than a UIImage object. Then, when you look at the NSData 's length property, your file size will match exactly. See Using ALAssetsLibrary and ALAsset take out Image as NSData for some sample code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM