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?
What is the "cracking" sound coming fro...
In recent years, as the concept of healthy eating...
Baidu search promotion is a pay-per-performance o...
Audit expert: Shen Yingjian Director of the Nutri...
One minute with the doctor, the postures are cons...
The job of an information flow advertising optimi...
[Smart Farmers] Urgent reminder! Is the venom com...
May 15th is the 32nd National Day for Persons wit...
[[120838]] GITC 2014 Global Internet Technology C...
Table tennis skills teaching video 137 episodes v...
Introduction: In order to do a good job in online ...
One minute with the doctor, the postures are cons...
The Details of History (all five volumes) The whe...
Leviathan Press: In 1974, researcher Larry Weiskr...
Internet advertising generally follows the follow...