Large amounts of memory allocated on setImage:

南笙酒味 提交于 2020-01-05 08:54:10

问题


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?


回答1:


I'm assuming you're setting your image to your image view using something like this:

[imgV setImage:[UIImage imageNamed:@"yourImg.png"]]

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]]];

Also, be sure to set your image to nil when you're done using it:

[imgV setImage:nil];

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:

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



来源:https://stackoverflow.com/questions/25559990/large-amounts-of-memory-allocated-on-setimage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!