This article continues the two remaining points on iOS performance optimization from the previous article:

  1. Optimization suggestions (startup, interface, energy consumption, etc.)
  2. Application of thin body

4. Optimization suggestions (startup, interface, energy consumption, etc.)

There are two types of startup: cold startup and hot startup. The startup time is divided into two parts: before the main function is executed, and after the main function is started

4.1 Startup Optimization Suggestions

4.1 Before main

Reduce dynamic libraries. Merge some dynamic libraries to reduce the number of Objc classes, classes, and selectors

4.2 Main function until application startup is complete

Time-consuming operations, not in the finishLaunching method, initialization, viewDidLoad, etc. of the initial display interface lifecycle functions

The influence of dynamic library to start time testing www.cocoachina.com/ios/2016112… Using dynamic Framework increases app startup time. If your number is around 25, the static framework startup time will increase by around 0.5 seconds compared to OC. Personally, I’m not very hopeful that iOS will improve the time it takes to load the framework, and there seems to be no hope that Apple will allow custom frameworks to live in memory, which may not be fixed any time soon.

Manually managing some external dependencies in a framework can work, but more complex package dependencies can be tricky.

If the company is demanding performance, 500ms May be unbearable. However, I think for most products, it is more uncomfortable to sacrifice the performance of 500ms than to use OC.

DYLD_PRINT_STATISTICS is set to 1

4.2 Interface Optimization

4.2.1 Principle of Caton

Frame drop

4.2.2 Evaluation of interface fluency

Interface optimization tips (I’ll write a separate article on interface optimization later)

Time-consuming operation, do not put in the main thread reasonable use of CPU and GPUCopy the code

CPU: Computes display content, such as view creation, layout calculation, image decoding, text drawing. These are generally UIKit issues, Apple uses Autolayout layout, GPU: will render the data calculated by CPU, view + data + frame rate = many images

[from] www.javashuo.com/article/p-y…

4.2.2.1 Color Blended Layers

Example Set the opaque property to true. All controls default to True. You can ignore setting an opaque color for the View, but you don’t have to specify white. Especially label. BackgroudColor

label.backgroundColor = [UIColor whiteColor];
label.layer.masksToBounds = YES;
Copy the code

You may wonder why the first line of the label is enough to set the background color. This is because if the content of the label is Chinese, the actual rendering area of the label is larger than the size of the label, and a sublayer is added to the outermost layer. If the outer gray of the edge of the label in the second line is not set, the layer is mixed red, so the second sentence should be added when the content of the label is Chinese. Single use label. Layer. MasksToBounds = YES is not going to happen off-screen rendering. Note: UIImageView is a special control, not only the container itself must be opaque, and the imageView content must also be opaque. If your image has a layer mixed red, first check whether there is a problem with its own code, if the code is correct, it is the image itself

4.2.2.2 rasterizer

Application: Rasterization is usually used when the image content remains unchanged. For example, setting shadows consumes a lot of resources for static content. If rasterization is used, it will certainly help improve the performance. Not applicable: If the content changes frequently, do not open this time, otherwise it will form a waste of performance. For example, we use tableViewCell, usually do not use rasterization, because the tableViewCell is drawn frequently, the content is constantly changing, if the use of rasterization, will form a large number of off-screen rendering performance decline.

4.2.2.3 Offscreen Rendering (Rounded Corners)

Shadow map shadow: use ShadowPath instead shadowOffset imageViewLayer. Setting the properties of ShadowPath = CGPathCreateWithRect (imageRect, NULL); Use GraphicsContex to generate a picture or view with rounded corners, here does not write the specific implementation process, required to baidu Copy, a lot of ready-made code.

4.3 Caton test:

An FPS of 50-60 is very smooth, anything below or above is abnormal. YYFPSLabel“YYLabel YYDemo

     YYFPSLabel *_fpsLabel = [YYFPSLabel new];
    [_fpsLabel sizeToFit];
    _fpsLabel.bottom = KScreenHeight - 55;
    _fpsLabel.right = KScreenWidth - 10;
    //    _fpsLabel.alpha = 0;
    [kAppWindow addSubview:_fpsLabel];
Copy the code

If the interface is complex, it is recommended to use AsyncDisplayKit for control drawing instead of Apple’s autolayout.

5. Apply weight loss

5.1 Slimming Resources

5.1.1 Lossless compression: format factory, image compressiontinypng.com/

5.1.2 Viewing THE CPU Architectures supported by static Libraries

Lipo - info libname. A. (or libname. Framework/libname)Copy the code

Merge static libraries

lipo  -create  libname-armv7.a   libname-armv7s.a   libname-i386.a  -output  libname.a
Copy the code

5.1.3 Static library split

lipo  libname.a  -thin  armv7  -output  libname-armv7.a
Copy the code

5.1.4 Compiler optimization

1. Strip Linked Product, Make Strings read-only, Symbols Hidden by Default set to YES 2. Disable exception support, Enable C++ Exceptions, Enable Objective-C Exceptions set to NO, Other C Flags add -fno-exceptionsCopy the code

5.2 LinkMap Parsing Tool

Github.com/huanxsd/Lin…

1. In XCode, enable the compilation option Write Link Map File XCode -> Project -> Build Settings -> Set the Write Link Map File option to yes and specify the linkMap storage location 2. After the project is compiled, find the Link Map file (TXT type) in the compilation directory default file address:  ~/Library/Developer/Xcode/DerivedData/XXX-xxxxxx/Build/Intermediates/XXX.build/Debug-iphoneos/XXX.build/Copy the code