如何针对特定目标文件大小优化UIImage调整大小? [英] How can I optimize UIImage resizes for a specific target filesize?

查看:77
本文介绍了如何针对特定目标文件大小优化UIImage调整大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于任何给定的文件数据大小,我希望能够调整(或压缩)UIImage的大小以适合该数据限制。这个问题不是,关于如何调整大小或如何检查文件大小...这是关于一种以高效方式获得此文件的算法。

For any given file data size, I want to be able to resize (or compress) a UIImage to fit within that data limit. This question is NOT about how to resize, or how to check file sizes... it is about an algorithm to getting this in a performant way.

已经在这里搜索了,我发现此线程,其中谈到降低图像jpeg的质量以线性或二进制算法。这不是很好的表现,最多只需要几十秒。

Searching here already, I found this thread which talks about stepping down the image jpeg quality in a linear, or binary algorithm. This isn't very performant, taking dozens of seconds at best.

我在iOS上工作,因此图像可以接近10MB(来自iPhone 4S)。我的目标虽然可变,但当前为3145728字节。

I am working on iOS so images can be close to 10MB (from iPhone 4S). My target, although variable, is currently 3145728 bytes.

我当前正在使用 UIImageJPEGRepresentation 进行一点压缩,但是为了达到我的低目标,对于这么大的照片,我似乎不得不失去很多画质。 UIImage 的大小和 NSData 的大小有关系吗?是否有一些函数可以让我说:

I am currently using UIImageJPEGRepresentation to compress a little, but to get to my low target it appears I would have to lose much quality for such a large photo. Is there a relation between UIImage size and NSData size? Is there some function where I can say something like:

area * X = dataSize

...并求解比例因子,以便我可以一次调整大小?

...and solve for a scale factor so I can resize in one shot?

推荐答案

在查看您链接到的线程后,我刚想到的一个想法是:压缩10MB图像将相对较慢。如何将大小调整为更小(以便压缩更快),然后执行压缩算法(从链接)。然后可以将其用作压缩10MB图像大小的指南?这样的想法是,同一图像的压缩率应该是相似的,与大小无关。

One idea I just had after looking at the thread you linked to: compressing a 10MB image is going to be relatively slow. How about resizing to be much smaller (so that compression is much faster), then performing the compression algorithm (from the link). This can then be used as a guide to the size of compressing the 10MB image? The idea being that the compression ratio should be similar for the same image, independent of size.

假设1000x1000像素的压缩大小为10MB,目标大小为3MB。

Let's say 1000x1000 pixels compressed is 10MB, target size is 3MB.

然后说,以相同质量压缩的较小的100x100像素(例如)为 C MB。然后在100x100图片上执行二进制搜索算法,直到大小= C *(3/10)。然后对1000x1000的图片使用这种压缩质量,以获取约3MB的图片。

Then say smaller 100x100 pixels (for example), compressed with same quality, is C MB. Then perform the binary search alg on the 100x100 image until size = C * (3/10). Then use this compression quality for the 1000x1000 image to get ~3MB image.

注意:我不知道这样做的效果如何-这只是一个建议。选择较小尺寸的图像(我使用过100x100)是一个猜测,还需要尝试一些东西。

Note: I have no idea how well this will work - it's just a suggestion. What size to pick (I've used 100x100) for the smaller-sized image is also just a guess and something would need to be experimented with.

这篇关于如何针对特定目标文件大小优化UIImage调整大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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