Inaccurate NSData size specified in bytes

I took a 13.3 MB image and added it to Xcode. I know that when compiling, Xcode performs some tricks to reduce the file size. I did this to check how large the image was, after converting to data:

UIImage *image = [UIImage imageNamed:@"image.jpg"]; NSData *data = UIImageJPEGRepresentation(image, 1.0); NSLog(@"length: %i", data.length); 

The length I received was 26758066 . If it is in bytes, then it reads to me as 26.7MB. How suddenly does the image appear? Is there any other way to get an image in the form of data without going through UIImage first?

EDIT: Further testing shows that this works, and outputs a data length of ~ 13.3 MB - the expected amount:

 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"image" ofType:@"jpg"]; NSData *data = [NSData dataWithContentsOfFile:filePath]; NSLog(@"length: %i", data.length); 
+8
ios objective-c iphone uiimage nsdata
source share
1 answer

What your code does is decompress the image in memory and then recompress it as JPEG, with the highest quality ratio ( q=1.0 ). That's why the image suddenly gets so big.

If you want to check the file stored in the resource bundle, query NSBundle for the full path to the file and use NSFileManager to read the file size. You can do the same manually on your Mac, just look at BUILD_PRODUCTS_DIR for your project.

+7
source share

All Articles