IOS correctly selects the image loading method, and ios correctly selects
Correct image loading methods can play a major role in memory optimization. There are three common image loading methods:
// Method 1 UIImage * imag1 = [UIImage imageNamed: @ "image.png"]; // method 2 UIImage * image2 = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @ "image.png" ofType: nil]; // method 3 NSData * imageData = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @ "image.png" ofType: nil]; UIImage * image3 = [UIImage imageWithData: imageData];
Method 1: imageNamed:
Why are there two ways to do the same? The advantage of imageNamed is that it can cache Loaded Images. Apple's documents contain the following statements:
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.
This method first searches for the Image Based on the specified name in the system cache. If the image is found, it is returned. If an image is not found in the cache, this method loads the image data from the specified file, caches it, and returns the result. For the same image, the system only caches it To the memory once, which is very advantageous for reuse of the image. For example, if you need to reload the same icon in a TableView, you can use imageNamed to load the image. The system will Cache the icon to the memory. each time you use the image in the Table, only the image pointer is directed to the same memory. In this case, using imageNamed to load images becomes very effective.
HoweverimageNamed
The image will be cached, and the image data will be stored in the memory. The memory of iOS is very precious. When the memory consumption is too large, the memory will be forcibly released, that is, memory warnings will be encountered. Releasing the image memory in the iOS system is troublesome and may cause memory leakage. For example, when the animationImages of a UIView object is an NSMutableArray dynamic array containing the UIImage object, and the animation is carried out frame by frame. When imageNamed is used to load images to a dynamic array NSMutableArray, this may cause memory leakage. The reason is obvious.
The second and third methods are essentially the same: imageWithContentsOfFile: And imageWithData:
imageWithContentsOfFile
: Only images are loaded. Image Data is not cached. images are loaded to programs as data. Therefore, you can use this method to reduce memory consumption when large images and usage are small.
The following describes the usage of the two methods:
NSString *path = [[NSBundle mainBundle] pathForResource:@”icon” ofType:@”png”]; UIImage *image = [UIImage imageWithContentsOfFile:path];
And:
NSString *filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:“png”]; NSData *image = [NSData dataWithContentsOfFile:filePath]; UIImage *image = [UIImage imageWithData:image]; //or = [UIImage imageWithContentsOfFile:filePath];
If you load a large image and use it only once, you do not need to cache the image. In this case, imageWithContentsOfFile is suitable, and the system will not waste memory to cache images.
However, if you often need to reuse images in the program, it is best to select the imageNamed method. This method can save the time for loading images from the disk each time.