River’s lake rumours

For those who are new to iOS, if they are not doing particularly complex UI and interaction, they may never even think about performance optimization in iOS. After all, the performance of iPhone is getting stronger and stronger, and for a common APP, UIKit is still the same UIKit and the same set of code. In the era of iphone4 and iphone5, there may be some lag, but now there are iPhone8 and 8Plus with the strongest CPU and GPU. The Caton thing, it doesn’t exist.

Why is the old man still making a fuss?

Hypocritical or hypocritical, after all, in addition to iphone8 and 8Plus, some users may still use iphone5, phone6, the user is God, god’s experience should be taken care of. And program the ape side often sit still a product manager, often have intention to not intentional of catching a wonderful inspiration, “hey, here to add a animation is better” “there is a good overall have a shadow” “here I think this effect can be more cool”, standing on the point of view of the company, standing on the APP, on the basis of the overall style, For unreasonable needs and inspirations, we programmers must fight and do not do it. However, if the inspiration greatly improves the user experience and increases the user’s fun, but it is actually implemented, it may have some impact on performance. At this time, we should not clench our teeth and frown and dare not respond. To gas shen Dantian, secretly transport, offering performance optimization method, can quickly complete the task, so as to pull back in front of the product sister.

Without further ado, performance optimization is here:

Texture

YYKit

Read up on the two books of secrets, the young man can be thousands of years, all rivers and lakes.

Some little xia said, the above techniques is bad digestion, carelessly, tens of thousands of lines of code, the amount is too much, kill the goose that how to use, and even finished practice will also feel a bit rushed, completely don’t know the inner operation, afraid of greenery in the future, can I practice some simple first, slowly but surely, until my profound basic skills in the future, always ready to practice, Can the world invincible.

Yes, yes, with such a generous mind for a young man, you will have a bright future ahead of you. Let my husband teach you what he has learned all along.

Fast break not only

The iPhone refreshes 60 times Per Second. At the high end, the Frames Per Second is 60. 1000ms divided by 60 is about 16.7ms, which means that each refresh only gives the iPhone 16.7ms time to process various transactions. The iPhone is easy to handle, the FPS is 60 at this point and the experience is silky smooth. However, when there are too many transactions, the iPhone cannot complete the processing in 16.7ms. What to do? The current frame will be discarded and wait for the next resubmission, while the content on the screen will remain unchanged.

Therefore, our goal is to be fast, and strive to make iPhone finish all the things we ordered within 16.7ms. The world is invincible, but only fast can not be broken. To make iPhone run fast enough, we need to distinguish the reasons why it is not fast enough and apply the appropriate medicine.

Inside and outside and repair

Martial arts, outside the body, inside the practice of qi, both inside and outside the repair, in order to remain invincible. IOS optimization, also when this as a breakthrough point, to complement the strengths.

CPU main external: object creation, object adjustment, object destruction, layout calculation, text calculation, text rendering, picture decoding, image drawing and other operations are performed on the CPU.

GPU main-in: Texture rendering, view blending, and off-screen rendering are all performed on the GPU.

If you want to make CPU, GPU speed fast enough, it is necessary to appropriately reduce their burden, work happy, natural speed up. Understand the respective division of labor, can be aimed at strengthening, make up for weaknesses.

For the CPU, my husband gives you some tips for getting started:

  1. Using manual Layout

Ui-related operations can only be performed on the main thread, or they crash for you to see, just so arbitrary. Apple probably didn’t expect apps to be so complex and more powerful than computers when they designed them, but there are so many apps now that it’s hard to modify them, or else open source frameworks would be useless. Since it can only be done on the main thread, it is often a better choice to create xiBs and storeboards manually on performance-sensitive pages. For those of you who are interested in the performance impact of Xib and StoreBoard, do your own search.

  1. The time-consuming and laborious operations are carried out in background threads

Non-ui-related operations, the choice is much, now CPU, has been multi-core, reasonable use of multi-threading, do left hand draw square, right hand draw a circle, often get twice the result with half the effort. For example, placing file reads and data computations on child threads gives the CPU more time to handle UI-related tasks.

  1. Cache Cell height

For the Cell with variable height, when the network data request is completed, the background thread calculates the height of the Cell and stores it in memory, so that the tableView does not need to be calculated many times when scrolling. Not only the height of the Cell, for example, there is a label with an unfixed number of lines in the Cell. If the height of the label is also calculated in advance and cached, the young man will have unexpected effects. Of course, this operation is also performed in the background thread.

  1. The actual size of the image is the same as the UIImageView size

When you set a large IMAGE on a UIImageView, you need to complete the resampling process in the main thread, which is time-consuming and memory consuming, so try to set the image size close to UIImageView.

For GPU old husband also teach you some introductory tricks:

  1. Reduce layer blending operations

When only one view is displayed on the screen, the view is displayed in whatever color it is, and there is no layer blending.

When multiple views are superimposed, there is no view blending if the top view is opaque and has a background color.

When the superposition of multiple views, in view of the above is translucent, so this time the GPU is mixed, put the transparent color with the view of color mixing after get a color is displayed on the screen, this step is the consumption of GPU resources, when the mixture of view more, the consumption of GPU resources.

So to reduce the GPU burden, we need to reduce layer blending.

Here the old man has a few tips, young xia can refer to:

  • UIView backgroundColor should not be set to clearColor, preferably the same color as superView backgroundColor.
  • Images avoid images with alpha channels, whether local or background returned images. What? The design girl said no, Sonny it’s all about your charm.
  1. ShouldRasterize is appropriate

If shouldRasterize is enabled, the layer will be rasterized into a bitmap, and the borders, shadows and other effects of the layer will be cached in the bitmap, so that the next time you use it, you don’t need to use it again.

Some young man heard this, this is good, that are shouldRasterize is not done, the effect and performance of both.

Wait a minute, the old man advised:

  • For cells with fixed contents, you are advised to use raster.
  • Rasterization is recommended for static content with complex effects.
  • With rasterizationScale, you can display good results on different screens.
  • If updating a layer that has been rasterized will result in an off-screen rendering, this is not good.
  • Do not overuse it, the system limits the size of the cache, beyond the cache size, resulting in off-screen rendering, is also very bad.

Small has becomes

Now the little xia has learned the performance optimization of the entry secrets, do both inside and outside repair, walk the iOS river’s lake has a certain skill alongside the body, in the future meet the product little sister will be able to stand up his chest, for iraqis.

To see what happens next, watch the next breakdown: iOS Performance Optimization (Intermediate)