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

标签 ios

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

重现步骤:

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

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

3)添加两个IBActions

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

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

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

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

5)impalement "-(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) impalement "- (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”图像并保存在您的资源中

最佳答案

“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 千字节以获得与上述数字的正确比率)。

关于ios - 当我使用 UIImagePNGRepresentation 或 UIImageJPEGRepresentation 将 UIImage 转换为 NSdata 时,图像大小增加太多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14411369/

相关文章:

ios - 如何根据 UItableView 中的内容调整标签

ios - 透明 ViewController 与 presentViewController 一起工作,但不与 pushViewController 一起工作

ios - 在显示之前将 ViewController 加载到 ContainerView 中,因此没有可见的过渡

ios - 如果我们从不使用分配指针,有什么办法可以访问已释放的对象?

ios - Swift - IOS - 限时功能

ios - iphone 7 不支持 css

ios - UISearchBar 后面可见的范围栏

objective-c - 使用 ABAddressBook 进行内存管理

iphone - 在 UISegmentedControl 的选定选项卡上是否可以使用不同样式的字体?

ios - UITextView中触发超链接文本点击和普通文本点击