当我使用UIImagePNGRepresentation或UIImageJPEGRepresentation将UIImage转换为NSdata时,图像大小增加太多

当我使用UIImagePNGRepresentation或UIImageJPEGRepresentation将UIImage转换为NSdata时,图像大小增加太多。

重现步骤:

1)打开Xcode并select新项目作为基于单一视图的应用程序

2)打开ViewController.xib并添加两个button,名称为i)testing在线图像ii)testing本地图像

3)添加两个IBActions

i) -(IBAction)ClickLocalImageTest:(id)sender; ii) -(IBAction)ClickOnLineImageTest:(id)sender; 

4)将“在线testing图像”连接到“ -(IBAction)ClickOnLineImageTest:(id)sender

和“testing本地图像”为“ -(IBAction)ClickLocalImageTest:(id)sender ;”

5) -(IBAction)ClickLocalImageTest:(id)sender-(IBAction)ClickLocalImageTest:(id)sender ”方法如下

 - (IBAction)ClickLocalImageTest:(id)sender { NSLog(@"*************Test Local Image****************\n"); NSString *path=[[NSBundle mainBundle] pathForResource:@"hero_ipad_retina" ofType:@"jpg"]; NSLog(@"Before testing image size is :<---- %u kb",[[NSData dataWithContentsOfFile:path] length]/1024); UIImage *img = [UIImage imageNamed:@"hero_ipad_retina.jpg"]; NSLog(@"UIImagePNGRepresentation: image size is---->: %u kb",[UIImagePNGRepresentation(img) length]/1024); NSLog(@"UIImageJPEGRepresentation with scale 1.0: image size is---->: %u kb \n",[UIImageJPEGRepresentation(img, 1.0) length]/1024); NSLog(@"*************Completed test****************\n\n\n\n"); } 

6) - (IBAction)ClickOnLineImageTest:(id)sender- (IBAction)ClickOnLineImageTest:(id)sender ”方法如下

 - (IBAction)ClickOnLineImageTest:(id)sender { NSLog(@"*************Test Online Image****************\n"); NSLog(@"Before testing image size is :<---- %u kb",[[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://images.apple.com/home/images/hero_ipad_retina.jpg"]] length]/1024); UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://images.apple.com/home/images/hero_ipad_retina.jpg"]]]; NSLog(@"UIImagePNGRepresentation: image size is---->: %u kb",[UIImagePNGRepresentation(img) length]/1024); NSLog(@"UIImageJPEGRepresentation with scale 1.0: image size is---->: %u kb \n",[UIImageJPEGRepresentation(img, 1.0) length]/1024); NSLog(@"*************Completed test****************\n\n\n\n"); } 

7)请从这里下载“hero_ipad_retina.jpg”图片并保存在名为“hero_ipad_retina.jpg”的资源中

7)现在在Xcode 4.0后面运行这个项目,在SDK上面运行IOS3.0

**

 Expected Results: 1)Click on "Test Online Image" button result should be as following *************Test Online Image**************** Before testing image size is :<---- 78 kb UIImagePNGRepresentation: image size is---->: 78 kb UIImageJPEGRepresentation with scale 1.0: image size is---->: 78 kb *************Completed test**************** 2)1)Click on "Test Local image" button result should be as following *************Test Local Image**************** Before testing image size is :<---- 78 kb UIImagePNGRepresentation: image size is---->: 78 kb UIImageJPEGRepresentation with scale 1.0: image size is---->: 78 kb *************Completed test**************** Actual Results: 1)Click on "Test Online Image" button result should be as following *************Test Online Image**************** Before testing image size is :<---- 78 kb UIImagePNGRepresentation: image size is---->: 480 kb UIImageJPEGRepresentation with scale 1.0: image size is---->: 180 kb *************Completed test**************** 2)1)Click on "Test Local image" button result should be as following *************Test Local Image**************** Before testing image size is :<---- 78 kb UIImagePNGRepresentation: image size is---->: 480 kb UIImageJPEGRepresentation with scale 1.0: image size is---->: 180 kb *************Completed test****************** 

我的问题 :

为什么它增加了它的大小? 以及将图像转换为NSData的优化方法是什么?

注意:请从这里下载“hero_ipad_retina.jpg”图片并保存在您的资源中

“hero_ipad_retina.jpg”是一个压缩的jpg图像

这一行:

 [[NSData dataWithContentsOfFile:path] length]/1024 

给它的压缩文件大小…

这一行:

 [UIImagePNGRepresentation(img) length]/1024 

解压缩图像并将其转换为无损文件格式的PNG。 它的大小不可避免地要大得多。

这一行:

 [UIImageJPEGRepresentation(img, 1.0) length]/1024 

解压缩图像并将其重新压缩为JPG表示。 您已经将质量设置为最大值(1.0),因此 – 与原始文件相比,无疑将文件压缩到更低的质量 – 您将获得更大的文件大小。 如果将质量设置为0.5,则会获得小文件大小(大约42K)

这是一个很好的提醒你为什么你应该谨慎对待JPEG图像。 每次访问JPEG图像时,都是不压缩的。 如果重新压缩 – 即使是在完全质量的情况下 – 也会降低图像的质量(因为每个有损压缩比前一个压缩更糟糕)。 图像(平面颜色,直线/对比边缘)会增加并且变得特别明显。 PNG总是比较安全的 – 在24位是无损的,在8位处理平坦的颜色区域是很好的。

更新

要在内存中获取图像的大小:

 NSUInteger sizeInBytes = CGImageGetHeight(image.CGImage) * CGImageGetBytesPerRow(image.CGImage); 

从这里你可以计算出PNG,JPG和原始文件的压缩率(用千字节除以1024得到与上述数字正确的比率)。