Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

length property returns wrong size(bytes) for an image

Tags:

ios

uiimage

I have a .png file in my resources folder.(actual size is 411 KB) When I convert the uiimage to nsdata and try accessing length property, it gives me wrong value.

Code...

UIImage *image = [UIImage imageNamed:@"sample.png"];

NSData *imgData = [[NSData alloc] initWithData:UIImageJPEGRepresentation(image, 1.0)];
int imageSize   = imgData.length;
NSLog(@"Image size in KB is %d",imageSize/1024); //-------- returns 631 KB

Please let me know if there is any other property which helps..


So here is my requirement.... I want to know the size of the image I pick from uimagepicker. The exact size of the image when I see it in the finder and the size which gets returned to me after picking it from the library is totally different... Is there any other property which can be used instead of length?

like image 817
B.V. Avatar asked Jul 15 '13 07:07

B.V.


1 Answers

You are converting a png to a jpeg, and so different file size should be expected.

If you wish to get the file-size of the original, png image, do the following.

NSString *path = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"png"];
NSData *rawData = [NSData dataWithContentsOfFile:path];
NSLog(@"%d", rawData.length);
like image 79
matt3141 Avatar answered Sep 29 '22 07:09

matt3141