使用UIImageJPEGRepresentation时,compressQuality应该是什么? [英] What should compressionQuality be when using UIImageJPEGRepresentation?

查看:1048
本文介绍了使用UIImageJPEGRepresentation时,compressQuality应该是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对用户库中的照片应用过滤器,然后将其写回磁盘。我正在使用 UIImageJPEGRepresentation 。此函数采用 UIImage compressionQuality 值介于0.0和1.0之间。因为我想保留原始质量,所以我将其设置为1.0。但我发现这实际上会创建一个文件大小比原始文件更大的图像文件,甚至更大。我注意到在Apple的Sample Photo Editing Extension应用程序中,他们总是将质量设置为0.9。我试过了,它输出的文件大小接近原始照片文件大小。这对我来说没有意义。有人可以解释一下吗?始终使用0.9是否合适? iOS相机是否使用该值对其进行压缩,因此0.9是原始质量?或者为什么0.9实际上不是1.0,如果没有更好的质量?

I would like to apply a filter to a photo from the user's library then write it back to disk. I'm using UIImageJPEGRepresentation. This function takes a UIImage and a compressionQuality value between 0.0 and 1.0. Because I want to preserve the original quality, I set it to 1.0. But I found this actually creates an image file that's larger in file size than the original, somewhat significantly larger even. I noticed in Apple's Sample Photo Editing Extension app they always set the quality to 0.9. I tried that and it does output a file size close to the original photo file size. This doesn't make sense to me. Can someone explain this? Is it appropriate to always use 0.9? Does the iOS camera compress it using that value and thus 0.9 is the original quality? Or why isn't 0.9 actually 1.0, if there's nothing that's better quality?

NSData *renderedJPEGData = UIImageJPEGRepresentation(image, 0.9f);


推荐答案

这是一个老问题,你可能已经工作了这对你自己来说,但如果有人仍然感兴趣,这可能会有所帮助。

This is an old question and you've probably worked this out for yourself, but if anyone's still interested, this might help.

(我不是JPEG专家)

(I'm not a JPEG expert)

UIImageJPEGRepresentation 没有考虑到过去图像的压缩方式,只是压缩它当时的图像。

UIImageJPEGRepresentation isn't taking into consideration how the image may have been compressed in the past, it's just compressing the image that it has at the time.

你提到一张来自用户图书馆的照片。用户可以在其库中拥有任何图像,在任何 compressionQuality 压缩。

You mention a photo that's "from the user's library". The user could have any image in their library, compressed at any compressionQuality.

尝试将图像保持在相同的文件大小,您需要知道它最初如何保存,即使您使用相同的 compressionQuality ,您可能会导致不同的文件大小,因为正在压缩的数据是不同的。

To try to keep the image at the same file size, you'd need to know how it was originally saved, and even if you use the same compressionQuality, you will likely result in a different file size because the data that's being compressed is different.

至于使用 compressionQuality 为0.9来保存图像,这取决于如何你很重视你的图像质量与你想要使用多少空间。 0.7似乎经常被认为是一个很好的平衡。

As for using a compressionQuality of 0.9 for saving images, it depends on how much you value the quality of your image versus how much you space you want to use. 0.7 seems to be often suggested as a good balance.

我不确定iOS相机应用程序压缩图像的数量(如果有的话)我不是确保它保证是最好的质量。如果它使用0.9,您可以随时创建一个拍摄照片并在保存之前压缩它的应用程序。

I'm not sure how much the iOS camera app compresses the image (if at all) and I'm not sure that it is guaranteed to be the best possible quality. If it is using 0.9, you could always create an app that takes a photo and compresses it less before saving.

我希望有所帮助。

这篇关于使用UIImageJPEGRepresentation时,compressQuality应该是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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