iOS 图像压缩算法 - 我将如何创建一个?

发布于 2024-12-26 10:15:55 字数 312 浏览 2 评论 0原文

我正在开发一个应用程序的一部分,该应用程序使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我太投入 2025-01-02 10:15:56

您需要反复试验——将图像大小调整为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文