iOS 图像压缩算法 - 我将如何创建一个?
我正在开发一个应用程序的一部分,该应用程序使用 ImagePickerViewController 来拍照/使用现有图片作为个人资料页面。我将图片(所有 JPEG)的分辨率尺寸减小到 480x320,然后对其进行精细压缩。问题是我无法确定哪些照片需要降低和压缩分辨率。
现在我想做的是编写一个算法,如果图片大于 480x320,则降低图片的分辨率,并调整图片的压缩级别,使其大小约为 30-50 KB。压缩级别将随着图片的大小而缩放,因此小于 30-50 KB 的任何内容都不会被压缩,任何超过 30-50 KB 的内容都会受到所需的压缩。
我将如何编写一个算法来处理这个问题?
I am working on a portion of an app that has uses an ImagePickerViewController to take pictures/use existing pictures for a profile page. I am reducing the size of the resolution of the pictures (all JPEGs) to 480x320 and than compressing it fine. The problem is that I have no way to determine which photos need to have resolutions reduced and compressed.
Now what I want to do is write an algorithm to reduce the resolution of a picture if it is larger than 480x320 and adjust the compression level of a picture so that it will be around 30-50 KB in size. The compression level will scale with the size of the picture so anything less than 30-50 KB has no compression, anything more has as much compression as needed to hit 30-50 KB.
How would I go about writing an algorithm to handle this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要反复试验——将图像大小调整为 480x320,然后将其保存为 JPEG,例如,质量为 95%。检查文件大小,如果太大,请在 90% 处重试。重复直到进入正确的范围。
这可能会相对较慢(几秒钟?),因此请在后台线程上执行此操作。
You'll need to do trial and error -- resize the image to 480x320, then save it as a JPEG at, say, 95% quality. Check the file size and if it's too big, try again at 90%. Repeat until you get into the right range.
This will potentially be relatively slow (several seconds?) so do this on a background thread.