App thin body

Resources thin body

  • usetinypngCompress PNG images. Video can be compressed in resolution with software such as Final Cut. For audio, lower the bit rate.
  • Non-required resource files can be placed on their own servers
  • Start diagram useLaunchScreen.storyboard, startup diagram in a project resources is actually quite a large proportion, but useLaunchScreen.storyboardJust set up an ImageView.
  • The IconFontIt is very convenient to use. If there are too many ICONS in the project or you need to change the icon color at any time, it is recommended to use it

Give up usingRealm

Realm is said to be the best performing mobile database available. But as you can see in the tripartite library, there is a significant amount of support for Realm, around 8M. Using FMDB, however, only 192KB is required, while CoreData is negligible.

Delete duplicate code

Review of duplicate code and remove useless open source libraries

Performance optimization

ImageWithContentsOfFile, Assets. Xcassets

  • Large images that occasionally need to be displayed should be placed in the project directory, not inAssets.xcassets; And useimageWithContentsOfFileLoading does not allow the system to cache
  • For small pictures that often need to be displayedAssets.xcassetsLet the system cache, useimageNamedloading

Use non-escape closures whenever possible

Non-escape closures are good for memory optimization, so use them whenever possible

NSSet, NSArray

NSSet (hash) is used to store objects and is a collection, just like NSArray. But unlike NSArray, which is an unordered set, which is stored in memory discontinuously, NSArray is an ordered set, which is stored in memory continuously. So NSSet is used when looking for an element in a collection, and NSArray is used when you need to loop through all the objects in the collection to find the desired target

Page caton

The principle of screen display image

  • CPU (central processor)

Object creation and destruction, object property adjustment, layout calculation, text calculation and typesetting, image format transcoding and decoding, image drawing (Core Graphics)

  • GPU (Graphics processing unit)

Texture Rendering (OpenGL)

  • FrameBuffer

  • 1.CPUCalculates the position and size of the control
  • 2. After the calculation is completedCPUThe data will be submitted toGPUTo render
  • 3. The GPU will convert the received data into the data format that can be displayed on the screen, and cache it inFrameBuffer
  • 4, thenVideo controllerfromFrameBufferThe read data is displayed indisplayon

Causes and solutions of caton

Due to the VSync mechanism, if the CPU or GPU does not complete the content submission within a VSync period, the frame will be discarded and displayed at the next opportunity, while the display will keep the previous content unchanged. That’s why the interface gets stuck.

As you can see from the above figure, whichever CPU or GPU blocks the display process will cause frame drops. Therefore, it is necessary to evaluate and optimize CPU and GPU pressure respectively during development.

Caton optimization -CPU

  • 1. Try to use lightweight objects, such as those that do not need event handlingCAlayerreplaceUIView
  • 2. Don’t skip too oftenUIVIewFor exampleframe,bounds,transformAnd other attributes to minimize unnecessary modifications
  • 3, try to calculate the layout in advance, adjust the corresponding layout at one time when necessary, do not modify the attributes for many times
  • 4,AutolayoutWould be better than setting directlyframeConsumes more CPU resources
  • 5. The size of the picture had better be exactly the sameUIImageViewthesizeconsistent
  • 6. Control the maximum number of concurrent threads
  • 7. Try to put time-consuming operations into child threads
  • 8. Text processing (size calculation, drawing)
  • 9. Image processing (decoding and drawing)

Caton optimization -GPU

  • 1. Minimize the number and level of views
  • 2,GPUThe maximum texture size that can be processed is 4096×4096, and once this size is exceeded, it will be occupiedCPUResources are processed, so the texture should try not to exceed this size
  • 3, try to avoid the display of a large number of pictures in a short time, as much as possible will be a composite picture display
  • 4. Reduce the number of transparent views (alpha<1) and set opaque to yes
  • 5. Try to avoid off-screen rendering

Off-screen rendering

This refers to creating a buffer on the GPU outside the current screen buffer for rendering operations

Causes of off-screen rendering:

  • shouldRasterize
  • Shadows
  • Edge Antialiasing
  • Opacity: group opacity
  • Rounded corners (triggered when used with maskToBounds)
  • The gradient

Off-screen rendering can be detected by Instruments’ Core Animation.

TableView tuning

  • Calculate ahead of timecell, cached in the corresponding data source model, reducedCPUCalculation time of
  • As low as possibleStoryboard,XibSuch as using degrees
  • Asynchronous rendering
  • Reduce the level
  • Use as little transparency as possible with views in cells
  • Avoid off-screen rendering