码迷,mamicode.com
首页 > 其他好文 > 详细

小图用 imageNamed: 大图用 dataWithContentsOfFile:options

时间:2014-11-10 21:30:47      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:des   http   io   ar   os   sp   for   div   on   


1down voteaccepted

If you‘re loading images like:
[UIImage imageNamed:@"myImage.png"];
Then the memory for this image will not deallocated, because imageNamed: cache the image. This method is useful for showing some small icons,avatars, etc. So, if you must show many big images, then use
UIImage *myImage = [UIImage imageWithData:[[NSBundle mainBundle] pathForResource:@"myImage" ofType:@"png"];
This will not cache the image and memory will deallocated when retain count will equal to 0 for that object.

Besides that, when you only push a view controller without pop it in future, then the view controller will never deallocated and will always take a place in memory until the app will not crashed/killed, or until the ViewController who holds that view controller will not deallocated.

So, always make sure that you deallocated the viewController if there is no more need of it (i.e. pop it if it was pushed, or dismiss it in case if it was presented modally) and then show your next view controller.

share|improve this answer
 
1  
Instead of using -dataWithContentsOfFile:, I would suggest using -dataWithContentsOfFile:options:, and use the NSDataReadingMappedIfSafe, so that you don‘t have the overhead of copying around the file‘s data to user-space until UIImage reads it. –  Richard J. Ross IIIJul 10 at 20:03

小图用 imageNamed: 大图用 dataWithContentsOfFile:options

标签:des   http   io   ar   os   sp   for   div   on   

原文地址:http://www.cnblogs.com/willbin/p/4088028.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!