The recent project is mainly LBS, which focuses on member positioning. Our UI design is as follows At first glance, it looks good and beautiful. Different people will display different avatars. But when people gather together, problems arise. When there are many people (such as shown in the picture above), the map can be felt to be stuck when sliding, and the unsmooth feeling is very torturous, so naturally we have to solve this problem (wait, don’t complain about why we don’t use map aggregation because this map has already been put on the ***, aggregation is not suitable for this problem discussion) analyze First, let's take a look at how I implemented this annotationView. Since this annotationView is a special shape (that is, it cannot be directly obtained by setting the rounded corners) and the pictures inside vary from user to user, the solution is to use layer.mask for masking. The code is as follows
Next, use the tool to analyze the problem. To analyze the performance, of course, select Instruments (I won't introduce the usage here). Open Core Animation and run the program. Slide the map to see the performance analysis as follows It turns out that the average frame rate is less than 30 frames, which is far from our target of 60 frames. Then use Debug Option to analyze it in depth Because of MKMapView, we are mainly concerned with these options here Color Blended Layers Color Misaligned Images Color Offscreen-Rendered Yellow The results of opening these options separately are as follows You can see There is no problem with Color Blended Layers, but this is normal because there is no transparent area due to the use of masks Color Misaligned Images is all correct except for the default avatar. This is because the image size on the server is inconsistent with the displayed size, which causes scaling. The default avatar is consistent, so there is no problem. Color Offscreen-Rendered Yellow is all due to the use of masks, which leads to a lot of off-screen rendering, which is also the main reason for the performance degradation solve Now that the cause of the problem has been found, how can we solve it? First of all, the mask is definitely not usable. Secondly, we need to preprocess the downloaded images to their actual size Then wouldn't it be ok to just combine the downloaded pictures into the final result we want to display? Try it
Then when using it, just simply call it as follows
See how the modified Instruments performs Color Blended Layers is unavoidable because what is displayed is a transparent image. However, due to the special nature of the map (the position of the avatar changes at a long interval, so it does not often trigger synthesis and there is no animation), so this is not a problem. Color Misaligned Images are no longer a problem because the avatars have been scaled to the same size. Color Offscreen-Rendered Yellow is fine because it is simply displaying an image and nothing needs to be rendered offscreen. Let's look at the frame rate Oh-Yeah~ Not only did the frame rate reach our target of 60 frames (because there are business logic threads running in the background, so it is not so stable), but the average running time has also dropped a lot. Even if we can display dozens more people on the map, it is no problem. summary Not only MKMapView, but also many places including UITableView can use the method mentioned in the article to optimize. The core point is synthesis + caching. Of course, since synthesis still consumes some resources, it is more suitable for small resources such as avatars. For graphics performance optimization, you can read this good article (if you don’t understand the Debug Option mentioned in the article, here is a detailed explanation) |
<<: How to make the next match-3 game?
>>: Xiaomi Vice President: Open-sourcing Android is the smartest decision Google has made
This article mainly makes a comprehensive knowled...
I have been collecting cases and organizing mater...
Why do you say that being coquettish is a man'...
In 2020, affected by the COVID-19 pandemic, the o...
With the vigorous development of the Internet sho...
Since the establishment of Douyin store groups, m...
For promoters, the most difficult time is the fir...
Although Android still has many problems, it is u...
A week before the Mid-Autumn Festival last year, ...
[Editor's Note] This article introduces 5 pre...
The shrinking mobile phone market [[414476]] In f...
An inventory of the top ten experiences of the ne...
In addition to allowing us to once again witness ...
I have been doing advertising optimization for a ...
Before you get your much-desired Apple Watch, are...