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

Refined user growth case!

ABtest is gaining more and more attention. Fast a...

Given a scenario, can Alipay recreate an Alibaba?

In the past few days, we have witnessed a miracle...

SAIC Volkswagen Teramont debuts

At this year's Guangzhou Auto Show, SAIC Volk...

SEM promotion: How important is the idea? 80% of bidders die on this!

After reading this article, you can improve in th...

Inventory of essential tools for new media operations (dry goods collection)

We also need to arrange a good-looking layout How...

Are viruses really alive?

© Vox Leviathan Press: From the perspective of hu...

Case Analysis | What is JD.com’s “Koi” information flow advertising like?

Today, the editor will share with you a JD.com &q...

Seven Tips to Increase App Revenue (Part 1)

Mobile marketing is a fluid ecosystem. If CPs wan...

From 3GS to 6S, Apple has caused us 9 times more trouble

Apple lets you listen to music, Apple lets you pa...

ZTE "Big Q" experience review: 1,000 yuan 4G can also be unique

In the increasingly competitive thousand-yuan smar...