如何通过数据大小缩小IOS中的UIImage [英] How to downscale a UIImage in IOS by the Data size

查看:93
本文介绍了如何通过数据大小缩小IOS中的UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在 iOS 中缩小 UIImage

我已经看到了下面的其他问题以及他们如何按尺寸缩小图像的方法。
调整图像尺寸Objective-C

如何在iphone中的objective-c中以编程方式调整图像大小

调整UIImage大小的最简单方法?

I have seen other questions below and their approach on how to downscale the image by size. Resizing Images Objective-C
How to resize the image programmatically in objective-c in iphone
The simplest way to resize an UIImage?

这些问题都是基于将图像重新调整为特定尺寸。在我的情况下,我希望根据最大尺寸重新调整/缩小图像。

These questions are all based on re-sizing the image to a specific size. In my case I am looking to re-size/downscale the image based on a maximum size.

例如,我想设置一个最大值 NSData 大小为500 KB。我知道我可以像这样得到图像的大小://检查返回图像的大小

As an example, I would like to set a maximum NSData size to be 500 KB. I know that I can get the size of the image like this:// Check the size of the image returned

NSData *imageData = UIImageJPEGRepresentation(image, 0.5);

// Log out the image size
NSLog(@"%lu KB",(imageData.length/1024));

我想做的是这里的某种形式的循环。如果尺寸大于我设置的最大尺寸,我想略微缩小图像,然后检查尺寸。如果尺寸仍然太大,请再次按比例缩小,然后再次检查,直到它低于最大设定尺寸。

What I would like to do is some form of loop here. If the size is greater than the maximum size that I set, I would like to scale down the image slightly, then check the size. If the size is still too large scale down again slightly then check again, until it is lower than the maximum set size.

我不知道最好的方法是什么是。理想情况下,我不希望始终将图像缩小到特定大小,但每次只能略微缩小图像。这样我可以拥有最大的(大小w / h)图像本身和最大尺寸(字节)。如果我一次只略微缩小,那么最好的方法是什么?

I am not sure what the best approach for this is. Ideally I do not want to scale down the image to a specific size all the time, but only slightly scale down the image each time. That way I can have the largest (size w/h) of the image itself and at its maximum size (bytes). If I scale down slightly only at a time, what would be the best way to accomplish this?

编辑
要确认,我想重新调整实际图像的大小,但重新调整图像大小,使其小于最大 NSData 长度。
例如:

- 检查 NSData 长度

- 如果高于我想通过UIImage的最大值进入方法

- 然后循环这个方法每次稍微调整实际图像大小的尺寸

-Until它在最大值 NSData 长度,然后返回图像?

EDIT To confirm, I am looking to re-size the actual image but re-size the image so that it is smaller than the maximum NSData Length. For example:
-Check the NSData Length
-If above the maximum I want to pass the UIImage into a method
-Then loop through this method slightly re-sizing the actual image size each time
-Until it is under the maximum NSData length, then return the image?

推荐答案

现在,你有一个例行程序说:

Right now, you have a routine that says:

// Check if the image size is too large
if ((imageData.length/1024) >= 1024) {

    while ((imageData.length/1024) >= 1024) {
        NSLog(@"While start - The imagedata size is currently: %f KB",roundf((imageData.length/1024)));

        // While the imageData is too large scale down the image

        // Get the current image size
        CGSize currentSize = CGSizeMake(image.size.width, image.size.height);

        // Resize the image
        image = [image resizedImage:CGSizeMake(roundf(((currentSize.width/100)*80)), roundf(((currentSize.height/100)*80))) interpolationQuality:kMESImageQuality];

        // Pass the NSData out again
        imageData = UIImageJPEGRepresentation(image, kMESImageQuality);

    }
}

我不建议递归调整大小图片。每次调整大小时,都会失去一些质量(通常表现为图像的软化,细节丢失,累积效应)。您总是希望返回原始图像并调整尺寸越来越小的尺寸。 (作为一个小问题,如果语句也是多余的。)

I wouldn't advise recursively resizing the image. Every time you resize, you lose some quality (often manifesting itself as a "softening" of the image with loss of detail, with cumulative effects). You always want to go back to original image and resize that smaller and smaller. (As a minor aside, that if statement is redundant, too.)

我可能会建议如下:

NSData  *imageData    = UIImageJPEGRepresentation(image, kMESImageQuality);
double   factor       = 1.0;
double   adjustment   = 1.0 / sqrt(2.0);  // or use 0.8 or whatever you want
CGSize   size         = image.size;
CGSize   currentSize  = size;
UIImage *currentImage = image;

while (imageData.length >= (1024 * 1024))
{
    factor      *= adjustment;
    currentSize  = CGSizeMake(roundf(size.width * factor), roundf(size.height * factor));
    currentImage = [image resizedImage:currentSize interpolationQuality:kMESImageQuality];
    imageData    = UIImageJPEGRepresentation(currentImage, kMESImageQuality);
}

注意,我没有触及 image ,原始图片,而是通过每次从原始图像调整大小来指定 currentImage ,每次都按比例递减。

Note, I'm not touching image, the original image, but rather assigning currentImage by doing a resize from the original image each time, by a decreasing scale each time.

顺便说一句,如果你想知道我的神秘 1.0 / sqrt(2.0),我试图在你的迭代之间做出妥协80%的因素和我希望能够以2的力量调整大小(因为当用2的幂完成时,减少保持更大的锐度)。但是使用你想要的任何调整因子。

BTW, if you're wondering about my cryptic 1.0 / sqrt(2.0), I was trying to draw a compromise between your iterative 80% factor and my desire to favor resizing by a power of 2 where I can (because a reduction retains more sharpness when done by a power of 2). But use whatever adjustment factor you want.

最后,如果你在巨大的图像上这样做,你可能会考虑使用 @autoreleasepool 块。您需要在仪器中的Allocations中分析您的应用程序并查看您的高水位标记的位置,因为在没有自动释放池的情况下,这可能构成相当积极的内存使用。

Finally, if you're doing this on huge images, you might think about using @autoreleasepool blocks. You'll want to profile your app in Allocations in Instruments and see where your high water mark is, as in the absence of autorelease pools, this may constitute a fairly aggressive use of memory.

这篇关于如何通过数据大小缩小IOS中的UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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