以字节为单位获取图像大小

时间:2013-03-11 08:13:21

标签: objective-c

我是以两种方式做的,两者都给出了错误的答案。

NSMutableArray *img=[[NSMutableArray alloc]init];
img=[memoInstance getTempImagesToSend]; //get nsmutableArray of images
NSData* myEncodedImageData=[img objectAtIndex:0];    
NSLog(@"IMAGE1:%@",[UIImage imageWithData:myEncodedImageData]); //i see image

NSLog(@"len:%lu",(unsigned long)[myEncodedImageData length]); //first way to show size

NSData *imgData = UIImageJPEGRepresentation([UIImage imageWithData:myEncodedImageData], 1);
NSLog(@"Size of Image(bytes):%d",[imgData length]); //second way to show

第一种方式:len:34898

第二种方式:Size of Image(bytes):47701

图像来自相册,带着iphone。它不能是47k ......应该像±500k

2 个答案:

答案 0 :(得分:0)

尝试更改

NSData *imgData = UIImageJPEGRepresentation([UIImage imageWithData:myEncodedImageData], 1);

NSData *imgData = [[NSData alloc] initWithData:UIImageJPEGRepresentation((your_ui_image), 0.5)];

答案 1 :(得分:0)

尝试以下方法:

int height = image.size.height; int width = image.size.width; 
int bytesPerRow = 4*width; 

if (bytesPerRow % 16) 
bytesPerRow = ((bytesPerRow / 16) + 1) * 16; 

int dataSize = height*bytesPerRow;