为什么 UIImage 保存到磁盘时比用于创建它的 NSData 重?
我在将 UIImage(从 HTTP 请求接收到的数据实例化)保存在文件中时遇到问题:文件很大,比 HTTP 请求返回的数据大小要重得多。
例如,当从给定 URL 下载图像时,检索到的数据长度约为 1.2MB。然后我用...实例化我的 UIImage
UIImage *myImage = [UIImage imageWithData:responseData];
,然后将图像保存到磁盘:
NSData *imageData = UIImageJPEGRepresentation(myImage, 1.0);
[imageData writeToFile:filePath atomically:YES];
生成的文件大小约为 5MB!
我知道我可以将接收到的数据保存到文件中,而不是首先实例化 UIImage,然后使用 UIImageJPEGRepresentation 检索其数据,但我很好奇为什么使用此方法时文件大小如此之大。
有什么提示吗?
提前致谢,
埃里克。
编辑:
我可以确认将响应数据保存到文件而不是首先实例化 UIImage 可以解决问题并生成与数据大小大致相同的文件。不过,我想知道为什么从 UIImage 保存时大小变化如此之大......
I'm facing a problem when saving an UIImage, instanciated from data received from an HTTP request, in a file : the file is HUGE, way heavier than the size of the data returned by the HTTP request.
For example, when downloading an image from a given URL, the retrieved data length is around 1.2MB. I then instanciate my UIImage with...
UIImage *myImage = [UIImage imageWithData:responseData];
...and then save the image to the disk with this :
NSData *imageData = UIImageJPEGRepresentation(myImage, 1.0);
[imageData writeToFile:filePath atomically:YES];
The resulting file weight is around 5MB !
I know I could just save the received data to the file instead of first instanciating an UIImage and then retrieve its data back with UIImageJPEGRepresentation, but I'm curious to know why the file size is so huge when using this method.
Any hint ?
Thanks by advance,
Eric.
EDIT :
I can confirm that saving the response data to the file instead of first instanciating an UIImage solve the problem and generate a file of around the same size than the data. Still, I wonder why the size change so much when saving from an UIImage...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我只能假设您的特定图像的 jpeg 效率低于原始数据 - 这可能是因为您将质量设置为 1.0 - 尝试将质量参数设置为较低的数字(0.7 很好,恕我直言)并查看文件大小下降:)
Sam
PS 看看这实际上会产生什么差异会很有趣 - 如果你尝试一下,你也可以在这里发布结果 - 谢谢!
I can only assume that the jpeg of your particular image is less efficient that the raw data - this is probably because you've set the quality to 1.0 - try setting the quality parameter to a lower number (0.7 is fine IMHO) and see the file size drop :)
Sam
PS It would be interesting to see what difference this actually makes - if you try it can you post the results here as well - thanks!