Delete useless images, de-duplication, compression, analysis, network download, Xcode configuration

To optimize the direction

1. Delete unnecessary images

LSUnusedResources

Disadvantages: 1. Can not solve the module delimit after the business picture reference, delete caution; 2. Use Xcassest to manage resource images. If the. Imageset and image name are different, the scan fails.Copy the code
(1) Open the Assets. Car file using Cartool; (2) Retrieve all resource pictures through find; (3) Through the script to traverse the executable file __TEXT, dynamic library, jsbundle, xib and other files to determine whether there are useless images in the IPA resource pictures.Copy the code
2. Establish a public resource library to remove duplicate images in each module

fdupes

Principle: Size comparison > Partial MD5 signature comparison > Full MD5 signature comparison > byte to byte comparison $brew install fdupes $fduPES -r ~/path# Search duplicate subfiles in terminal display
$ fdupes -Sr ~/path               # Search duplicate subfiles & display the size of each duplicate file in terminal display
$ fdupes -Sr ~/path > ~/log.txt   Log to the specified path folder
Copy the code
3. Compress the image

Lossless compression ImageOptim

XCode will recompress PNG images during compilation to generate CgBI format that Apple Dad likes. It will add the deleted metadata again. In order to optimize image decoding, Reduce unnecessary GPU and CPU overhead;Copy the code

May refer to: imageoptim Xcode’s built-in (de)optimization PNG compression and iOS apps Optimizing App Assets Working with Wide Color

Imageoptim Xcode’s Built-in (de)optimization: COMPRESS_PNG_FILES = NO; If the test is set to NO, recompress will still be applied.

The following 10 large images are selected for the test. These 10 images are compressed by ImageOptim, and other images in Assets. Car are opened by Cartool to check file size. If the image is not compressed, there will be no difference in size between the original image and the image in IPA.

Lossy compression tinypng

How it works: Quantize similar colors, convert 24-bit PNG images to 8-bit PNG images && remove unnecessary metadata. It includes the deletion of unnecessary metadata mentioned above && PNG24 -> PNG8. From the above, we know that only one optimization point will take effect by reducing color data, and the 70% compression ratio of the official website will also be greatly reduced. Disadvantages: image quality, compressed after the designer check.Copy the code

TinypngMac Release tingpngMac AppKey

Conclusions: (1) Using imageOptim lossless compression, images in Assets.car can be 100% recompress to the original image size, while images not in Assest. Car cannot be 100% recompress to the original image size; (2) JPG not put into assets. car will not be affected by Recompress and will increase; (3) The compressed image of imageOptim will not be invalid by Recompress and will not be increased. (4) Lossy compression is effective, but the effect is greatly reduced by recompress.

4. Analyze the image size proportion in the IPA
In view of the above one delete two to three pressure, three plate axe after the need to calm down to analyze the current IPA package picture size number composition. Through the data sampling analysis of the company's App, 4% of the images occupy 66% of the space. Use TestFlight to test the installation package and reduce the size by 31 MB. Continue to optimize and delete the 4% image, or transfer to the Internet to download the installation package size optimization is more impressive. If there are no similar problems, ignore themCopy the code
5. Go to the Internet for download

For iOS 8 support, you’ll need to develop your own management tools

Since iOS9 began to support the use of On Demand Resources, online introduction of many, no further details.

6. Build Settings Enable ASSETCATALOG_COMPILER_OPTIMIZATION space
Build Settings -> ASSETCATALOG_COMPILER_OPTIMIZATION -> Enable space to optimize 30%-40% of Assets. Car sizeCopy the code
7. Differences between XCode 10 vs. XCode 9 Assets.car

The size of Assets. Car packaged with XCode 10 is about 30% larger than the size of XCode 9. Normal, App Hillclimbing actual seismic range installation package size does not change, after being packaged with the test company App Xcode 10, the unused stack size will be 10% smaller than the stack size shown below on the AppStore. “assets.car” size nearly doubled while using Xcode 10 GM seed

8. Use Iconfont for unified image colors
Optimization strategy

(1) Give priority to Internet download; (2) Make full use of the Asset Catalog and place pictures in it whenever possible; (3) After Step 2, package and generate IPA package to analyze whether there is a large picture in Assets. Car, for example, more than 20KB; (4) There are large images after Step 3. If the alpha channel is used and tinypng is used to compress the images, only 3x images are reserved in the bundle for use. If only 2x images can be used, it is better.


Tool post

1. Quickly count the number && size of pictures in the project
PNG &&. JPG of the current path
$ find $PWD | egrep '\.(png|jpg)$'
       
PNG &&. JPG of the current path
$ find $PWD | egrep -c '\.(png|jpg)$'    

PNG &&. JPG file size of current path
$ find $PWD | egrep '\.(png|jpg)$' | xargs ls -l | awk '{print $5}' | awk '{sum1 += $1}END{print sum1}'.

# Because the image name is not standard, there is a space, Linux default use space partition, so first output.txt and then change
$ find $PWD | egrep '\.(png|jpg)$' > OUTPUT_FILE_PATH.txt       
$ cat OUTPUT_FILE_PATH.txt | tr '\n' '\ 0' | xargs -0 ls -l | awk '{print $5}' | awk '{sum1 += $1}END{print sum1}'.           

-maxdepth 1 set the depth of the query.
$ find $PWD -maxdepth 1 | egrep '\.(png|jpg)$'
Copy the code
2. View the Assets. Car file

Method 1: Cartool decompresses the Assets. Car file to a specified path

# https://github.com/steventroughtonsmith/cartool
# output_dir must be created first
$ ./cartool /path/to/Assets.car /path/to/outputDirectory
Copy the code

Method 2: AssetCatalogTinkerer can directly view the images from Assets. Car

# https://github.com/insidegui/AssetCatalogTinkerer

# http://stackoverflow.com/questions/22630418/analysing-assets-car-file-in-ios
Copy the code
3. Determine whether to use Alpha for PNG images

Image processing imagemagick

$ brew install imagemagick
$ identify IMAGE_PATH               # View picture information
$ identify -format %A IMAGE_PATH    # Blend uses alpha channel, Undefined does not use alpha channel
Copy the code
4. Check the compression ratio of ipA packets
unzip -lv xxx.ipa
Copy the code

reference

  1. GMTC shared Slides of Didi Chuxing’s iOS slimming practice
  2. Dry goods | headlines today iOS installation package size optimization – train of thought and practice