在setImage上分配了大量的内存: [英] Large amounts of memory allocated on setImage:
问题描述
我有一个以编程方式创建的UIImageView([[UIImageView alloc] init]
).在调用setImage:
方法之前,应用程序的内存一直处于检查状态.有什么想法吗?
I have a UIImageView that was created programmatically ([[UIImageView alloc] init]
). The app's memory stays in check until the setImage:
method is called. Any thoughts?
推荐答案
我假设您正在使用以下方法将图像设置为图像视图:
I'm assuming you're setting your image to your image view using something like this:
[imgV setImage:[UIImage imageNamed:@"yourImg.png"]]
使用该问题的原因是该应用 缓存 这些图像.如果要避免缓存图像,请使用imageWithContentsOfFile:
:
The problem with using that is that the app caches these images. If you'd like to avoid caching images, use imageWithContentsOfFile:
:
[imgV setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"yourImg.png" ofType:nil]]];
另外,在使用完图像后,请确保将其设置为nil
:
Also, be sure to set your image to nil
when you're done using it:
[imgV setImage:nil];
过去我对此有疑问,这是我从苹果公司收到的一封电子邮件中的一些文字,以回应TSI:
I have had issues with this in the past, and here's some text from an email I got back from Apple in response to a TSI:
在很多情况下,您都在使用API UIImage + imageNamed:加载图像,但是您应该注意,即使在返回的UIImage对象为 已发布.用-imageWithContentsOfFile替换对imageNamed的调用: 如下所述,是一种确保完全控制您的应用的 内存中的图像数据
There are quite a few cases where you’re using the API UIImage +imageNamed: to load images but you should be aware that imageNamed caches its image data even after the returned UIImage object is released. Replacing calls to imageNamed with -imageWithContentsOfFile: as outlined below is a way to ensure full control over your app’s image data in memory
这篇关于在setImage上分配了大量的内存:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!