【问题标题】:When i am using UIImagePNGRepresentation or UIImageJPEGRepresentation for converting UIImage into NSdata, the image size is too much increased当我使用 UIImagePNGRepresentation 或 UIImageJPEGRepresentation 将 UIImage 转换为 NSdata 时,图像大小增加了太多
【发布时间】:2017-11-10 21:28:47
【问题描述】:

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

复制步骤:

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

2)打开 ViewController.xib 并添加两个按钮,名为 i)Test Online Image ii)Test Local image

3)添加两个IBAction

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

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

4)将“测试在线图片”连接到“-(IBAction)ClickOnLineImageTest:(id)sender

和“测试本地图像”到“-(IBAction)ClickLocalImageTest:(id)sender;”

5)执行“-(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 {
     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)请从here下载“hero_ipad_retina.jpg”图片并保存在名为“hero_ipad_retina.jpg”的资源中

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

**

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 的优化方法是什么?

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

【问题讨论】:

  • 哇。我读到的第一个问题看起来像苹果雷达......不知道我对此有何感受。
  • 您已经重复了两次本地图像测试...
  • 感谢我编辑了我的问题
  • @GovindaraoKondala +1 为您提出问题的方式..我想知道

标签: ios


【解决方案1】:

“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 imageRep 时,都在解压缩。如果您然后重新压缩 - 即使以全质量 - 您正在降低图像的质量(因为每次有损压缩都比以前更差)。图形图像(单色、直边/对比边缘)会增加伪影并变得特别明显。 PNG 总是更安全 - 它在 24 位时是无损的,而在 8 位时则擅长处理纯色区域。

更新

获取内存中图像的大小:

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

据此,您可以计算出 PNG、JPG 和原始文件的压缩率(千字节除以 1024 得到与上述数字的正确比率)。

【讨论】:

  • 即使我从本地或在线读取,大小差异也可能小于 20 到 30%。但如果这里超过 300 % ?
  • @HelpMeToHelpYou 这是意料之中的。这就是在线使用 jpeg 的原因 - 它可以将文件大小减少多达 10 倍甚至更多(完全取决于质量设置)。如果您在预览中打开原始 jpeg 并导出为 (1) PNG,您将得到相同的结果; (2) JPG 副本。使用压缩设置,您会发现文件大小和质量存在巨大差异。
  • 谢谢。假设我阅读本地或在线 png 图像,那么大小是否相同?
  • @HelpMeToHelpYou 文件来自哪里没有区别,大小是一样的。一个好的经验法则是将本地图像存储为 PNG,在线图像存储为 JPG。如果您访问在线图像以在本地保存并打算对其进行操作,则应考虑将其转换为 PNG 以进行本地存储(如果您不对图像进行任何更改,则可以将其保留为 JPG)。
  • @Reza.Ab - JPEG 不支持透明度。您将获得较小的文件大小,但重新打开 jpeg 版本时会丢失透明度。
猜你喜欢
  • 2013-02-20
  • 1970-01-01
  • 2015-08-17
  • 1970-01-01
  • 2017-01-17
  • 2019-02-06
  • 2017-08-30
  • 2017-03-30
  • 2017-09-12
相关资源
最近更新 更多