在内存限制下调整UIImage的大小 [英] Resize UIImage under memory constraints

查看:127
本文介绍了在内存限制下调整UIImage的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Web资源(> 300MBs)下载了大量的UIImage,当我尝试渲染时,由于内存导致应用程序崩溃。我正在尝试使用以下代码调整图像的大小:

I have a massive UIImage downloaded from a web resource (>300MBs) which when I attempt to render is causing an app crash due to memory. I am trying to resize the image using the following code:

+ (UIImage *)imageWithImage:(UIImage *)image scaled:(float) scale {
    //UIGraphicsBeginImageContext(newSize);
    CGSize size = (CGSize){scale * image.size.width, scale * image.size.height};
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

问题是,正如您可能已经猜到的那样,这实际上需要渲染图像,从而导致相同的崩溃。有没有办法在不给系统带来如此大内存压力的情况下调整大图像的大小?

The problem is, as you may have guessed, that this requires actually rendering the image and thus causes the same crash. Is there a way to resize an large image without placing such an enormous memory strain on the system?

推荐答案

问题是正在将图像保存在内存中。将其下载到磁盘,并使用ImageIO框架直接从磁盘读取缩略图(较小的大小),而无需将完整大小的图像保存在内存中。

The problem is that you are holding the image in memory. Download it to disk and use the ImageIO framework to read a "thumbnail" (smaller size) directly from disk without ever having to hold the full-size image in memory.

这篇关于在内存限制下调整UIImage的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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