The optimization of mobile applications mainly depends on FPS (page smoothness) performance, memory usage, etc. Off-screen rendering is also a common problem. This article focuses on the common factors that lead to off-screen rendering and their solutions. So why does off-screen rendering cause performance issues? In OpenGL, there are two ways of GPU screen rendering: On-Screen Rendering and Off-Screen Rendering. On-Screen rendering does not require the creation of a new cache or the opening of a new context, and has better performance than off-screen rendering. However, due to the limitations of current screen rendering (only its own context, limited screen cache, etc.), off-screen rendering is used when current screen rendering cannot solve some problems. The entire off-screen rendering process requires switching the context environment, first switching from the current screen to the off-screen, and then switching the context environment back after the end. This is why performance is consumed.
The factors that trigger off-screen rendering include cornerRadius (setting rounded corners), shadows, masks, edge antialiasing, group opacity, shouldRasterize, etc. As for the tool Instruments' Core Animation for detecting off-screen rendering, I won't go into details. This article mainly introduces the solution for setting rounded corners and shadows. Set rounded corners Conventional practice:
Here are two solutions to avoid off-screen rendering 1. Add a sublayer to the top layer of the view to cover the view and its subviews. Set the layer's image to just cover the required rounded corners, and the image color is exactly the background color of the parent view of the view to achieve the desired effect. github address
After downloading this category, you can directly drag it into the project to use it. It is very convenient to call. However, when using it, you will find that these three APIs all need to pass a parameter cornerColor (the background color of the parent view), which also causes the limitation of this function. That is, if the color of the parent view is not a solid color, this method is not applicable at this time. Similarly, if the color of the parent view changes, the implementation code is not so elegant, as shown in the figure below, which is a bit embarrassing. Here comes the second solution.
2. By modifying layer.mask, first create a vector-based path through Bezier curves and pass it to CAShapeLayer for rendering. The path is closed, and then the drawn Shape is assigned to layer.mask. The layer outside the Mask range will not be displayed to achieve a rounded corner effect. The code implementation is very simple, as follows:
Effect picture:
Setting up shadows Conventional practice:
Optimization plan: Avoid modifying shadowOffset directly. Instead, call setShadowPath to provide a CGPath to the view's layer and provide the shape of the rendered View to Core Animation, which will reduce off-screen rendering calculations.
Supplement: When the shape of the view using the shadow changes, the shadowPath does not change with the bounds property of CALayer, so after the bounds of the layer change, you need to manually update the shadowPath to make it adapt to the new bounds. |
<<: WeChat ID can be changed at will? Official: It's just a bug
>>: Are mobile programmers having an easy time in 2017?
A week before the Mid-Autumn Festival last year, ...
Another Spring Festival is here. During this joyo...
[[171830]] Recently, iOS developer Steven Trought...
As the saying goes, "Toothache is not a dise...
I guess many optimizers have suffered from creati...
The POS machine used always jumps code, which caus...
1. Learn to analyze competitive products. Competi...
[51CTO.com original article] While most technical...
The greatest value of operations is to achieve th...
If you walk into the greenhouse of Wuhan Botanica...
With the strategic upgrades of major platforms an...
When you are lying in bed getting ready to fall a...
Screen: operate: Sound Effects: Plot: Experience:...
"Every time a festival comes, copywriters wo...
With the introduction of various national policie...