imageNamed的限制

来源:互联网 发布:mac 代理软件 编辑:程序博客网 时间:2024/05/06 08:22

[UIImage imageNamed:@"icon.png"]

用上面的方法加载图片有问题.

这种方法在application bundle的顶层文件夹寻找由供应的名字的图象 。 如果找到图片,装载到iPhone系统缓存图象。那意味图片是(理论上)放在内存里作为cache的。

如果图片多了,就傻了. 图片cache极有可能不会响应 memory warnings and release its objects

使用图片的时候一定要小心的alloc和release

 

 

推荐使用下面的方法做处理:

NSString *path = [[NSBundle mainBundle] pathForResource:@"icon" ofType:@"png"];

myImage = [UIImage imageWithContentsOfFile:path];

 

 ========================================================================

--  方法一

NSString *fileLocation = [[NSBundle mainBundle] pathForResource:fileName ofType:extension]; 
NSData *imageData = [NSData dataWithContentsOfFile:fileLocation]; 
 
[UIImage imageWithData:imageData];

 

-- 方法二

[UIImage imageNamed:fullFileName]

 

The imageNamed: method does cache the image, but in many cases that's going to help with memory use. For example, if you load an image 10 times to display along with some text in a table view, UIImage will only keep a single representation of that image in memory instead of allocating 10 separate objects. On the other hand, if you have a very large image and you're not re-using it, you might want to load the image from a data object to make sure it's removed from memory when you're done.

If you don't have any huge images, I wouldn't worry about it. Unless you see a problem (and kudos for checking Object Allocation instead of preemptively optimizing), I would choose less lines of code over negligible memory improvements.

 

================================================================================================

区别一

imageNamed: 只能读取包文件下的图片,不能读取Documents文件夹中的图片文件。

区别二:内存考虑
更重要的是,imageNamed 是要catch 图片