As project development progresses and versions iterate, there will always be some invalid image resources in the project, which often increase the compilation cost and package size. An average iOS project will have about one-third of its images unused. Commonly used methods are: 1. Use tools; 2. Use scripts; 3. Write the code for the picture name when adding a picture; The principle of the script & tool is roughly like this: search for the image name in the project, and if the image name is not used, it is considered that the image is not used and listed. This approach is not very accurate: 1. When using imageview for animation (i.e. imageView.animationImages), most developers like to use a for loop to add all images, which will cause the used images to be listed; 2. When using different resource packs, if two resource packs have the same name, but the images in one resource pack are not used, the unused images will not be listed; There are many such cases. Some people would say to use the third method, but adding it manually is time-consuming, which is the following scenario: For example, we want to track the number of times each view controller is shown to the user in the application: Of course, we can add tracking code in viewDidAppear of each view controller; but this is too cumbersome and requires writing duplicate code in each view controller. Creating a subclass may be an implementation method, but it requires creating subclasses of UIViewController, UITableViewController, UINavigationController and other view controllers in UIKit at the same time, which will also generate a lot of duplicate code. The above paragraph is the opening description of Method Swizzling. It is true that manual addition will inevitably result in omissions, so we need to use Apple's own method to handle them centrally. We can modify the calling method of UIImage through Method Swizzling, add a method to print the image (or path) in UIImage, and then write it to a file. At the end of the project, delete the images that do not appear in the file (you can use script deletion, which is convenient and accurate). In this case, we can write the following Method Swizzling, as shown in the code:
In this way, you only need to call [UIImage initialize] when Appdelegate starts to print out all the images used in imageNamed:. If you need to print all the used images, you only need to Method Swizzle all the class methods and instance methods in UIImage. In this way, after testing the project once, all the used images can be saved in an image list. You only need to use the scripting language to delete the images that are not in the image list in the project. Note: The Chinese versions of "Objective-C Runtime 4: Method Swizzling" and "Method Swizzling" missed a code comment in the original book:
by Adorable Dean at Nanjing, Jiangsu, China This article was first published on Adorkable Dean's blog. Please indicate the original author when reprinting it. If you have better insights on this article, you can contact me via WeChat. Conflict of interest: All software involved in this article are tools used by the author on a daily basis, and there is no advertising fee. |
>>: 11 common mistakes that new entrepreneurs make
Which kind of tea is the best for removing oil? I...
Lu Songsong is a well-known figure in our blog ci...
How does seo make money? How to make money with S...
The 13th UI Zero-Base Advanced Course of Plum Sup...
On June 19, 1997, the "Galaxy-III" para...
On August 27, YouTube officially launched the gam...
According to our observation, brands mostly face ...
When collecting paper tickets such as train ticke...
Venus is a terrestrial planet in the solar system...
Today we are still talking about ASO . When it co...
Toyota Motor President Akio Toyoda said on Wednes...
[[182653]] Not long ago, LinkedIn introduced the ...
If you want to host your project, consider GitLab...
The opposite of “our enemies know us better than ...
Have you noticed that fruits are getting sweeter ...