iOS图片加载方式的区别

正确选择图片加载方式能够对内存优化起到很大的作用

常见的图片加载方式有下面三种

// 方法1  
// imageNamed:
UIImage *imag1 = [UIImage imageNamed:@"image.png"];  

// 方法2  
// imageWithContentsOfFile:pathForResource:ofType:
UIImage *image2 = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image.png" ofType:nil]];  

// 方法3  
// dataWithContentsOfFile:pathForResource:ofType:
NSData *imageData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image.png" ofType:nil]];  
UIImage *image3 = [UIImage imageWithData:imageData]; 

第一种方法:imageNamed:

This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already in the cache, this method loads the image data from the specified file, caches it, and then returns the resulting object.

第二种方法和第三种方法本质是一样的:imageWithContentsOfFile:和imageWithData: