UIImage loading picture Images. xcassets loading method effect
- 2021-07-24 11:51:20
- OfStack
How UIImage loads pictures and how Images. xcassets affects the loading method
Picture cache
Depending on whether the created object is cached into system memory, there are two options for creating UIImage objects:
Cache: + imageNamed:, just pass in the filename. Extension (optional). Do not cache: + imageWithContentsOfFile:, you must pass in the full name of the file (full path + file name).Note that for a method with caching 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 picture data is loaded from bundle, the object is created and returned. If the corresponding picture data does not exist, return nil.
Images.xcassets
Images. xcassets is packaged in app and appears in bundle as an Assets. car file. Its function is to:
Automatically identify @ 2x and @ 3x pictures, and manage pictures with the same content but different resolutions. Pictures can be clipped and stretched.
UIImage Loads picture resources from bundle
Attention
1. Image resources in Images. xcassets can only be loaded through the imageNamed: method, and the image path cannot be obtained through pathForResource: ofType: of NSBundle. Therefore, Images. xcassets is only suitable for storing image resources commonly used in the system and occupying small memory.
2. imageNamed: The method can also load picture resources in the root directory.
3. To take advantage of the imageWithContentsOfFile: method to load pictures without caching, you must place the picture resource in the root directory.
4. Compared with jpg, iOS supports png better. For example, if you load a picture from a place other than Images. xcassets, you must add an extension after the file name, for example:
// pic.jpg Be in the root directory
[UIImage imageNamed:@"pic"]; // Error, the picture did not load correctly
[UIImage imageNamed:@"pic.jpg"]; // Correct
Thank you for reading, hope to help everyone, thank you for your support to this site!