The last article compared the performance of several ways of UIImage loading (iOS UIImage loading image performance analysis).

In terms of performance, loading images from Asset is 9 times better than loading the logical directory from imageNamed, and 2 times better than loading images from the logical directory imageWithContentsOfFile. Among them, using imageNamed to load logical directory images has the worst performance and the greatest impact on App performance.

In the actual development process, many developers may put the image in the logical directory and use imageNamed to load the image. They don’t see any impact on App performance. As the amount of code increases, this problem accumulates and App performance becomes challenging.

Project background

I tried to make some improvements in this area after accepting a stock project. My goal was to find a performance drag that was acceptable for loading images using imageWithContentsOfFile. The main thing was to find a way to load images from a logical directory using imageNamed.

There is a problem

There are already a lot of images in the code. Some functions may have been abandoned, but the images still exist. In addition, it is difficult to analyze whether imageNamed is an image loaded with Asset or a logical directory from the perspective of the code.

solution

Here are the stacks for the three loading modes:

1. ImageNamed loads the Asset resource



ImageNamed loads the logical directory resource

3. ImageWithContentsOfFile loading logic resources directory



First of all, the way imageWithContentsOfFile loads images is completely different from the stack of the previous two methods. My point is to distinguish the difference between imageNamed loading different resources.

As can be seen from the function stacks in figures 1 and 2, both are in

[CUICatalog _resolvedRenditionKeyFromThemeRef:withBaseKey:scaleFactor:deviceIdiom:deviceSubtype:displayGamut:layoutDirection:sizeCla ssHorizontal:sizeClassVertical:memoryClass:graphicsClass:graphicsFallBackOrder:]Copy the code

The function path is consistent, but the next call method diverges.

1 method called

[CUIStructuredThemeStore canGetRenditionWithKey:]
Copy the code

2 method called

[CUIMutableStructuredThemeStore canGetRenditionWithKey:]
Copy the code

From the Instrument, you can see that the time difference is very large. The difference between the branch gave me ideas to distinguish between the two, if I can record call [CUIMutableStructuredThemeStore canGetRenditionWithKey:] code, I can distinguish between imageNamed exactly where the loading pictures.

Use breakpoints to achieve stack fetching

I built in the middle of the project [CUIMutableStructuredThemeStore canGetRenditionWithKey:] symbols of breakpoints, and then run the whole project, by looking at the function of stack to find the location of the loading method using imageNamed mistake. But in the process of actual operation found that [CUIMutableStructuredThemeStore canGetRenditionWithKey:] not only in the UImage method will be called, some of the other systems approach, UINavigation, for example, is called during initialization. The most serious is a [UIImage imageNamed:] loading logic catalogue pictures of operation process, several times (in decades) call [CUIMutableStructuredThemeStore canGetRenditionWithKey:]. This makes the debugging process impossible.

My goal is to print the stack once when I read the [UIImage imageNamed:] method. To do this, I added a conditional judgment to the breakpoint and increased

[CUICatalog _resolvedRenditionKeyFromThemeRef:withBaseKey:scaleFactor:deviceIdiom:deviceSubtype:displayGamut:layoutDirection:sizeCla ssHorizontal:sizeClassVertical:memoryClass:graphicsClass:graphicsFallBackOrder:]Copy the code

Become a flow:



After testing, the above method can find the corresponding code function stack used by imageNamed error, but because the breakpoint function is turned on, the App will be stuck in the test process, but the use will not be affected.

The following breakpoint screenshot, you can parameters, please leave a message if you have any questions.