There are three ways to load images:
1. The image is cached in Asset
UIImage *image = [UIImage imageNamed:imageName];Copy the code
2. The image is in the logical directory with cache
UIImage *image = [UIImage imageNamed:imageName];Copy the code
3. Images are stored in the logical directory without caching
UIImage *image = [UIImage imageWithContentsOfFile:path];Copy the code
Performance analysis
Compare the performance of the first image load. Test case loaded 50 150*150 images, test model: iphone5, iOS 10.3.3, test tool, time Profiler.
Compare test results:
Among them,
1. The image is stored in Asset with cache. Total time: 52ms
UIImage *image = [UIImage imageNamed:imageName];Copy the code
2. The image is in the logical directory with cache, total time: 477ms
UIImage *image = [UIImage imageNamed:imageName];Copy the code
3. The image is in the logical directory without cache, total time: 99ms
UIImage *image = [UIImage imageWithContentsOfFile:path];Copy the code
As you can see from the above results, loading images with Asset provides the highest performance, with caching capabilities.
Using imageNamed to load logical directory resources is inefficient, but it is difficult to avoid developers’ misuse of the project. How to find the misuse of imageNamed to load logical directory images can refer to iOS UIImage to find performance drag
supplement
UIImage loading image decoding performance analysis