How UIImage loads images and its impact on loading methods
Image cache
Depending on whether the created objects are cached into system memory, there are two types of methods to create UIImage objects:
- Cache: + imageNamed:, just pass in the file name. extension (optional).
- Not cached: + imageWithContentsOfFile:, the full name of the file (full path + file name) must be passed in.
Notice,For a method with cache function, the steps to create an object are as follows:
- Find a specific UIImage object in the cache pool according to the image file name, enter the existence, and return this object.
- If it does not exist, the image data is loaded from the bundle, the object is created and returned.
- If the corresponding image data does not exist, return nil.
After the app is packaged, it appears in the bundle as a file. Its function is:
- Automatically recognize @2x and @3x images, and manage images with the same content but different resolutions uniformly.
- The pictures can be cut and stretched.
UIImage loads image resources from bundle
Notice
The image resources in the imageNamed: method can only be loaded, and the image path cannot be obtained through the pathForResource:ofType: of NSBundle. Therefore, it is only suitable for storing image resources commonly used in the system and occupying small memory.
: The method can also load the image resources in the root directory.
3. To use the imageWithContentsOfFile: method to load images in a non-cache form, the image resource must be placed in the root directory.
4. Compared with jpg, iOS supports png better. For example, if you load a picture from other places, you must add an extension after the file name, for example:
// In the root directory[UIImage imageNamed:@"pic"]; // Error, the image failed to load correctly[UIImage imageNamed:@""]; // correct
Thank you for reading, I hope it can help you. Thank you for your support for this site!