5

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?

4

3 回答 3

4

您正在将 png 转换为 jpeg,因此应该预期不同的文件大小。

如果您希望获得原始 png 图像的文件大小,请执行以下操作。

NSString *path = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"png"];
NSData *rawData = [NSData dataWithContentsOfFile:path];
NSLog(@"%d", rawData.length);
于 2013-07-15T07:23:59.640 回答
1

尝试这个:

unsigned int len = [data length];
uint32_t little = (uint32_t)NSSwapHostIntToLittle(len);
NSData *byteData = [NSData dataWithBytes:&little length:4];
于 2013-07-15T07:23:18.630 回答
0

当您加载图像时,您将其解压缩。当您创建“imgData”时,图像没有使用相同的算法重新压缩。没有理由期望两者具有相同的大小。

于 2013-07-15T10:57:10.690 回答