Technical dry goods: Statistics on the use of pictures in the project

Technical dry goods: Statistics on the use of pictures in the project

[[155011]]

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:

  1. #import "ADeanImage+Hook.h"  
  2. #import  
  3. #import  
  4. @implementation UIImage (Hook)
  5. + ( void )initialize
  6. {
  7. static dispatch_once_t onceToken;
  8. dispatch_once(&onceToken, ^{
  9. [self adeanImageHook];
  10. });
  11. }
  12. + ( void )adeanImageHook
  13. {
  14. [self imageNameHook];
  15. }
  16. + ( void )imageNameHook // Class method calling method  
  17. {
  18. Class class = object_getClass((id)self);
  19. SEL originalSelector = @selector(imageNamed:);
  20. SEL swizzledSelector = @selector(adean_imageNamed:);
  21. Method originalMethod = class_getClassMethod( class , originalSelector);
  22. Method swizzledMethod = class_getClassMethod( class , swizzledSelector);
  23.       
  24. BOOL didAddMethod = class_addMethod( class , originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
  25. if (didAddMethod)
  26. {
  27. class_replaceMethod( class , swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
  28. }
  29. else  
  30. {
  31. method_exchangeImplementations(originalMethod, swizzledMethod);
  32. }
  33. }
  34. + (UIImage *)adean_imageNamed:(NSString *)name
  35. {
  36. UIImage *image = [self adean_imageNamed:name];
  37. [self printImageNameToLocalWithImageName:name];
  38. return image;
  39. }
  40. + ( void )printImageNameToLocalWithImageName:(NSString *)name
  41. {
  42. #ifdef ADeanForTest  
  43. {
  44. // Print image address  
  45. ADeanLog(@ "adean_msg: imagefile %@" , IMAGEFILEFILE);
  46. FILE *fp;
  47. const   char *imageFilePath =[IMAGEFILEFILE UTF8String];
  48. const   char *cImageName = [[NSString stringWithFormat:@ "%@\n" , name] UTF8String];
  49. /*Open the file*/  
  50. if ((fp = fopen(imageFilePath, "a" )) == NULL)
  51. {
  52. ADeanLog( "Error opening the file, please check if the file exists\n" );
  53. }
  54. else  
  55. {
  56. }
  57. fputs(cImageName,fp);
  58. fclose(fp);
  59. }
  60. #endif  
  61. }
  62. @end

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:

  1. // When swizzling a class method, use the following:  
  2. // Class class = object_getClass((id)self);  
  3. // ...  
  4. // Method originalMethod = class_getClassMethod(class, originalSelector);  
  5. // Method swizzledMethod = class_getClassMethod(class, swizzledSelector);

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.

<<:  Alibaba was reported to have acquired Sina: "The godfather is about to become the biological father"

>>:  11 common mistakes that new entrepreneurs make

Recommend

You are also using this kitchen gadget! Be careful of "explosion"

Air fryer As a new kitchen "artifact" H...

How to perform exceptionally well during an argument?

Source: A Brief History This article has been aut...

Hongmeng alone cannot save Huawei phones

In May 2019, facing overseas bans, Huawei solemnl...

E-commerce operations: analysis of the promotion system!

E-commerce has been integrated into our lives and...

Where did all those guys who made routers last year go?

In October 2013, Lao Wang contacted Zhai Kejun thr...

How can APP promotion and operation achieve explosive growth in users?

Preface: The competition among Internet products ...