【发布时间】:2009-08-18 13:37:13
【问题描述】:
我有一个 UIImage,我正在使用 imageWithData: 实例化它:(数据是使用 [NSData dataWithContentsOfFile:] 从包中加载的)。
然后我像这样绘制图像:
NSData *imageData = [NSData dataWithContentsOfFile:fileLocation];
UIImage *myImage = [UIImage imageWithData:imageData];
//These lines are superfluous from what I can tell, replacing with
//UIImage *myImage = [UIImage imagedNamed:imageName]; very soon.
[myImage drawAtPoint:CGPointMake(0,0)];
//myImage will be released at the end of the run loop
我的问题是:创建的 UIImage 是自动发布的。当 UIImage 被绘制到视图然后 UIImage 被解除分配不存在时,在内存方面会发生什么。显然,在视觉上,图像仍然存在,因为它已被绘制到上下文中。
如果 UIImage 有效且已被绘制到视图,内存使用量是否会翻倍,然后在 UIImage 被释放后返回到与仅存在一个 UIImage 相同的数量?
现在走另一条路。
如果我使用 [UIImage imageNamed:] 来实例化图像,那么 UIImage 类有自己的各种图像缓存,并且只会保存特定图像的一个真实实例(无论创建了多少 UIImage 实例来表示那一张图片)。
我的另一个问题是:如果我将缓存中的图像绘制到上下文然后释放 UIimage(通过运行循环结束时的自动释放),会发生什么情况?图像是否留在缓存中消耗内存?是否因为没有其他 UIImage 实例在使用它而被移除?
任何帮助都会很棒,谢谢!
【问题讨论】:
-
能否提供更详尽的代码?
-
老实说,没有更多的代码可以提供。不过,我会添加不存在的内容。
标签: iphone memory-management caching uikit uiimage