For the difference between the two initialization methods of the UIImage Class Object, uiimage Initialization
1. imageNamed:
1 UIImage *image = [UIImage imageNamed:@"1"];
Class Method of UIImage
When reading an image for the first time, put the image in the cache and then read the image with the name directly from the cache. If the image with the name does not exist in the cache, load the image to the cache before returning the object. If we need to frequently load some disposable images in a short period of time, we 'd better not use this method.
Advantage: it is convenient and quick. It is only a little slow when used for the first time. It will be faster when used later;
Disadvantage: memory will be wasted if it is used only once in the current project.
2. imageWithContentsOfFile: And initWithContentsOfFile:
1 // obtain the path of an image resource in the package. 2 NSString * oldImagePath = [[NSBundle mainBundle] pathForResource: @ "image" ofType: @ "png"]; 3 // obtain the image by Path 4 UIImage * image1 = [UIImage imageWithContentsOfFile: oldImagePath]; // Class Method 5 UIImage * image2 = [[UIImage alloc] initWithContentsOfFile: oldImagePath]; // object Method
ImageWithContentsOfFile: -- class method of UIImage
IntiWithContentsOfFile: -- Object method of UIImage
Each time the image is read Based on the path, it is directly loaded from the file system and returned without going through the system cache. It does not consume memory. If the loaded image is used only once in the project, select this method. When receiving a memory warning, the system may release the memory for storing images in the UIImage and re-load the image as needed next time.