UIImage与大图像大小 - 内存问题 - 崩溃 [英] UIImage with Large image size - Memory Problem - Crash

查看:259
本文介绍了UIImage与大图像大小 - 内存问题 - 崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想加载并显示尺寸较大的图片( .jpg, .png)。 1800x1200或2000x1800(宽x高)。
如果我在UIImageView中显示这样大的图像(1800x1200或2000x1800),它也会消耗大量的内存,应用程序崩溃。
同样根据苹果的文档( http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIImage_Class/Reference/Reference.html ),我们应避免创建大小超过1024 x 1024的UIImage对象。
一个解决方案是调整图像大小。我使用以下代码应用逻辑调整图像大小。

  UIGraphicsBeginImageContext(newSize); 
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

这可以很好地调整图像大小。但是当它加载大型图像在设备上下文来调整大小,它消耗大量的内存一秒钟,因为它的应用程序崩溃。
所以这种方法不工作。



也可以在这里( http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIImage_Class/Reference/Reference.html )链接,Apple表示


应避免创建大小超过1024 x 1024的UIImage对象大量的内存,这样的图像将消耗,你可能会遇到问题,当使用图像作为纹理在OpenGL ES中,或当绘制图像到视图或图层。此大小限制不适用,如果你执行基于代码的操作,例如通过将图像绘制到基于位图的图形上下文来调整大于1024 x 1024像素的图像大小。实际上,您可能需要以这种方式调整图像大小(或将其分割成几个较小的图像),以便绘制你的观点之一。


可以有人详细说明这个大小限制不适用于如果你执行基于代码的操作,例如通过将其绘制到基于位图的图形上下文来调整大于1024 x 1024像素的图像大小。?如何在基于位图的图形上下文上绘制图像?什么是位图支持的图形上下文?它会帮助我,如果有人可以共享示例代码。
提前感谢!

解决方案

UIGraphicsBeginImageContext()创建一个位图支持的图形上下文限制的发生是因为UIViews使用CALayers渲染,而CALayers渲染为GPU上的纹理多核,GPU具有最大纹理尺寸(例如CGBitmapContextCreate)。这似乎是2044像素在iPhone 2G / 3G和推测iPod Touch 1G / 2G)。他们已经在3GS和以后的设备上增加了它,可能是因为来自相机的图片更大。



这说,你应该能够操纵各个相机大小的图像没有问题;崩溃表明您在其他地方使用了大量内存(可能是内存泄漏?)。


I wanted to load and display image(.jpg,.png) having large size e.g. 1800x1200 or 2000x1800 (width x height). If I display such large size images (1800x1200 or 2000x1800) in UIImageView, it also consume lot of memory and application is getting crashed. Also as per Apple's documentation (http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIImage_Class/Reference/Reference.html), we should avoid creating UIImage objects that are greater than 1024 x 1024 in size. One solution is to resize image. I have applied logic to resize image using following code.

UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

This works fine and resizes the image. But when it loads large image in device context to resize, it consume lot of memory for a second and because of it application is crashing. So this approach is not working. Please share if you have better approach to resize without crashing it.

Also in this (http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIImage_Class/Reference/Reference.html) link, Apple said that

"You should avoid creating UIImage objects that are greater than 1024 x 1024 in size. Besides the large amount of memory such an image would consume, you may run into problems when using the image as a texture in OpenGL ES or when drawing the image to a view or layer. This size restriction does not apply if you are performing code-based manipulations, such as resizing an image larger than 1024 x 1024 pixels by drawing it to a bitmap-backed graphics context. In fact, you may need to resize an image in this manner (or break it into several smaller images) in order to draw it to one of your views.". 

Can somebody elaborate more on "This size restriction does not apply if you are performing code-based manipulations, such as resizing an image larger than 1024 x 1024 pixels by drawing it to a bitmap-backed graphics context." ? How to draw image on bitmap-backed graphics context? What is bitmap-backed graphics context? It would help me if somebody can share sample code. Thanks in advance!!

解决方案

UIGraphicsBeginImageContext() creates a bitmap-backed graphics context (under the hood it calls CGBitmapContextCreate).

The "restriction" happens because UIViews are rendered using CALayers, and CALayers are rendered as textured polys on the GPU, and the GPU has a maximum texture size (this appears to be 2044 pixels on iPhone 2G/3G and presumably iPod Touch 1G/2G). They've increased it on the 3GS and later devices, presumably because pictures from the camera are bigger.

That said, you ought to be able to manipulate individual camera-sized images without a problem; a crash suggests that you're using a lot of memory elsewhere (possibly a memory leak?).

这篇关于UIImage与大图像大小 - 内存问题 - 崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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