Reputation: 73
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?
Upvotes: 5
Views: 969
Reputation: 4423
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);
Upvotes: 4
Reputation: 47699
When you loaded the image you decompressed it. When you created "imgData" the image did not get recompressed with the same algorithm. There would be no reason to expect the two to have the same size.
Upvotes: 0
Reputation: 1730
try this:
unsigned int len = [data length];
uint32_t little = (uint32_t)NSSwapHostIntToLittle(len);
NSData *byteData = [NSData dataWithBytes:&little length:4];
Upvotes: 1