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
ABtest is gaining more and more attention. Fast a...
In the past few days, we have witnessed a miracle...
At this year's Guangzhou Auto Show, SAIC Volk...
After reading this article, you can improve in th...
National Day Holiday Many people choose to travel...
July 28 news, according to foreign media reports,...
We also need to arrange a good-looking layout How...
© Vox Leviathan Press: From the perspective of hu...
How to dominate the Baidu screen with 10,000 word...
In his book The Meaning of Work, James Suzman, an...
Today, the editor will share with you a JD.com &q...
Mobile marketing is a fluid ecosystem. If CPs wan...
Apple lets you listen to music, Apple lets you pa...
In the increasingly competitive thousand-yuan smar...
According to the latest data from IDC, Lenovo'...