I took a 13.3MB image and added it to XCode. I'm aware that when compiling, XCode performs some tricks to bring down the file size. I did this, to test how big the image now was, after being converted into data:
UIImage *image = [UIImage imageNamed:@"image.jpg"];
NSData *data = UIImageJPEGRepresentation(image, 1.0);
NSLog(@"length: %i", data.length);
The length I got back was 26758066
. If that's in bytes, then it reads to me as 26.7MB. How is the image so big suddenly? Is there another way for me to get the image in data form without going through UIImage first?
EDIT: Further testing reveals that this works, and brings out a data length of ~13.3MB - the expected amount:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"image" ofType:@"jpg"];
NSData *data = [NSData dataWithContentsOfFile:filePath];
NSLog(@"length: %i", data.length);
What your code is doing is decompressing the image into memory and then recompressing as JPEG, with the highest quality ratio (q=1.0
). That’s why the image is suddenly so big.
If you want to check up on the file as stored in the resource bundle, ask NSBundle
for the full file path and use NSFileManager
to read the file size. You can do the same thing by hand on your Mac, just take a look into the BUILD_PRODUCTS_DIR
for your project.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With