imageNamed:与imageWithContentsOfFile:区别

imageName:

加载图片成功后会在内存中缓存图片,这个方法用一个指定的名字在内存中查找并返回一个图片对象,如果缓存中没有找到图片对象,则从指定的地方加载图片,然后缓存对象,并返回这个对象

imageWithContentsOfFile:

仅加载图片不缓存.

NSString *path = [[NSBundle mainBundle] pathForResource:@”icon” ofType:@”png”];
myImage = [UIImage imageWithContentsOfFile:path];

imageWithData:

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

比较

  1. imageWithContentsOfFile:当应用程序需要加载一张大的图片并只使用一次,则没必要缓存这个图片。
  2. imageName:对于同一图像,系统只把图像缓存到内存一次,这对于图像的重复利用是有好处的。例如,当需要在UITableView中重复加载同一个图标时,使用imageName:加载图像,系统会把图标缓存到内存,在 table 每次使用那个图标时,只会把图片指针指向同一块内存,这种情况下使用imageName:会非常高效
  3. imageWithData:图像会被系统以数据方式加载到程序,当不需要重用该图像,或者图像需要以数据方式存储到数据库中,或者通过网络下载一幅很大的图片时,尽量使用imageWithData:。同 imageWithContentsOfFile:,只要 UIImage 对象没有被释放,再次渲染时就不会调用 ImageIO 解码方法

参考

  1. imageName:与imageWithContentsOfFile:区别

你可能感兴趣的:(imageNamed:与imageWithContentsOfFile:区别)