Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inaccurate NSData size given in bytes

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);
like image 371
Andrew Avatar asked Mar 27 '13 10:03

Andrew


1 Answers

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.

like image 148
zoul Avatar answered Oct 31 '22 18:10

zoul