使用 UIImageJPEGRepresentaion() 将 UIImage 转换为 NSData 并将其保存到文件会增加文件大小(以字节为单位) [英] Converting UIImage to NSData with UIImageJPEGRepresentaion() and saving it to a file increases the file size in bytes

查看:54
本文介绍了使用 UIImageJPEGRepresentaion() 将 UIImage 转换为 NSData 并将其保存到文件会增加文件大小(以字节为单位)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求,用户可以从 camera roll 中选择图像,我的应用程序需要优化图像并将其上传到服务器.我正在使用 ALAssets 库和一个自定义选择器,供用户从 camera roll 中选择一张照片.我能够完美地实现这一点,但文件大小增加有问题.

I have a requirement in which the user can select an image from camera roll and my application needs to optimize the image and upload it to a server. I am using ALAssets library and a custom picker for the user to select a photo from camera roll. I am able to achieve this perfectly but have a problem with the file size getting increased.

问题如下:
当用户选择 1.7MB 的照片时(这是我尝试邮寄照片时 iOS 报告的完整大小),当我直接从 ALAssetRepresentation<获取字节时/code> 的那个 ALAsset 并将其转换为 NSData 数据的大小也是 1.7MB.但是,当我使用以下代码从 ALAssetRepresentation 获取 UIImage 时..

The problem is as follows:
When the user selects a photo, of 1.7MB (That is the full size as reported by the iOS when I try to mail the photo), when I directly get the bytes from the ALAssetRepresentation of that ALAsset and convert it to NSData the size of data is 1.7MB too. But, when I get a UIImage from the ALAssetRepresentation using the following code..

UIImage *selImage = [UIImage imageWithCGImage:[assetRepresentation fullResolutionImage] scale:[assetRepresentation scale]orientation:[assetRepresentationorientation]];

UIImage *selImage = [UIImage imageWithCGImage:[assetRepresentation fullResolutionImage] scale:[assetRepresentation scale] orientation:[assetRepresentation orientation]];

并使用UIImageJPEGRepresentation()UIImage 转换为NSData,压缩质量= 1.0f,NSData 大小几乎变成原始大小的两倍(3.2MB).

and converting the UIImage to NSData using UIImageJPEGRepresentation() with compression quality = 1.0f, the NSData size becomes almost double the original size (3.2MB).

理想情况下不应该只有 1.7MB 吗??

Shouldn't it ideally be 1.7MB only??

正如预期的那样,当我将它保存到文件时,文件大小仍然显示为 3.2MB(我通过导入沙盒应用程序的文档文件夹使用 xcode 4.2 检查了它).但有趣的是,当我在 Mac 上使用 Preview 打开文件并检查字节大小时,它显示 1.7MB(原为 3.2MB),这非常令人困惑.

As expected, when I save it to a file, the file size still says 3.2MB (I checked it using xcode 4.2 by importing the sandbox app's documents folder). But the interesting thing is when I open the file with Preview on my mac, and check for the byte size, it says 1.7MB (was 3.2 MB) which is pretty confusing..

请帮忙..

提前致谢.

推荐答案

您指定的压缩质量是什么?这与原始图像上使用的相同还是不同?如果不一样,用同样的压缩因子试试看大小是否一样.

What compression quality are you specifying? Is this the same or different from what was used on the original image? If it is different, try it with the same compression factor and see if the size is the same.

NSData * UIImageJPEGRepresentation (
   UIImage *image,
   CGFloat compressionQuality
);

这篇关于使用 UIImageJPEGRepresentaion() 将 UIImage 转换为 NSData 并将其保存到文件会增加文件大小(以字节为单位)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆