iOS文章iOS开发

iOS 加载图片imageNamed 和imageWithCon

2016-03-17  本文已影响913人  奋斗的蜗牛
  1. imageNamed,其参数为图片的名字。
    这个方法用一个指定的名字在系统缓存中查找并返回一个图片对象如果它存在的话。如果缓存中没有找到相应的图片,这个方法从指定的文档中加载然后缓存并返回这个对象。因此imageNamed的优点是当加载时会缓存图片。所以当图片会频繁的使用时,那么用imageNamed的方法会比较好。
  1. imageWithContentsOfFile,其参数也是图片文件的路径。
    仅加载图片,图像数据不会缓存。因此对于较大的图片以及使用情况较少时,那就可以用该方法,降低内存消耗。

获取图片

NSString *path = [[NSBuddle mainBuddle] pathForResource:@"resourceName" oftype@"resourceType"];
UIImage *image = [[UIImage imageWithContentsOfFile:path];
UIImage *image = [UIImage imageNamed:@"imageName"];

以及

NSString *filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:“png”];  
NSData *image = [NSData dataWithContentsOfFile:filePath];  
UIImage *image = [UIImage imageWithData:image]; 
//or = [UIImage imageWithContentsOfFile:filePath]; 
上一篇 下一篇

猜你喜欢

热点阅读