iOS picture setting fillet performance optimization

Source: Internet
Author: User

Problem

Rounded corners are good, but if used improperly, it is your number of frames killer, especially when it appears on the scrolling list. Let's see how the fillet can ruin your fluency.

Measured

Layer.cornerradius

I created a simple uitableview view, added 2 Uiimageview instances for each cell, and set the following for the Uiimageview instance

AImageView.layer.cornerRadius = aimageview.frame.size.width/2.0;

AImageView.layer.masksToBounds = YES;

Run as follows:


You guess, what is the frame rate of the scroll now?


Has fallen to 45 frames per second, this frame rate has made people feel less smooth, if less than 40 frames per second, ordinary users will be aware of the obvious is not smooth. When I increased the Uiimageview instance of cell to four


Now the frame rate is below 30 frames per second.


This frame rate, if present on the first screen, is enough to lead your app into the junk level experience. Now I'm going to make the size of the Uiimageview instance smaller.


The average frame rate increases by about 3 frames per second.


Here the size of the view and fillet has no effect on the frame rate and the number is the core output of the damage.

Principle

The reason for the slow frame rate above is actually off-screen Rendering (off-screen rendering). Off-screen rendering is a good thing, but frequent off-screen rendering is time-consuming.

Off-screen Rendering

From

screen rendering, which means that the GPU creates a new buffer outside the current screen buffer for rendering operations. By one of the above conclusions the size of the view and fillet has no effect on the frame rate and the number is the core of the damage

Output AH. It is possible to know that the off-screen rendering time is taking place on the off-screen action, rather than rendering. Why is it so time-consuming to get off screen? The main reasons for this are creating buffers and context switches. The cost of creating a new buffer is not counted.

Big, the most expensive is context switching.

Context Switches

On

The context switch is a fairly time-consuming operation, whether in the GPU rendering process or a process switch that has been familiar. First I want to save the current screen rendering environment and then switch to a

A new drawing environment, requesting the drawing of resources, initializing the environment, and then starting a drawing, after the drawing is finished, destroy the drawing environment, as needed to switch to on-screen

Rendering or start a new off-screen rendering before repeating the operation. Describes the rendering operation of a mask.


One

The mask takes two off-screen rendering and one-time home-screen rendering. Even ignoring expensive context switches, a mask needs to be rendered three times before it can be displayed on the screen, which is already a normal view showing 3 time-consuming, if

Plus the context switch below, a mask is more than 30 times times the time-consuming operation of normal rendering. Ask me this 30 times times how this data come out? When I was in cell Uiimageview,

Increases to 150, and the number of frames drops to 28 frames per second when the rounded corners are removed. Although not very accurate, it is at least dozens of times times more time-consuming to reflect the mask operation.

The first type: Set the Cornerradius of the Calayer

 imageView.image = [UIImage imageNamed:@"img"]; imageView.image.layer.cornerRadius = 5; imageView.image.layer.masksToBounds = YES;

This setting triggers off-screen rendering and compares consumption performance. For example, when you have more than 10 avatars on a page that have rounded corners

Will obviously feel the lag.
This is the most commonly used, but also the most consumption of performance.

Note: After ios9.0, the fillet setting of the Uiimageview is optimized, uiimageview this setting rounded corners
Off-screen rendering is not triggered, and off-screen rendering is triggered before ios9.0. And UIButton will trigger off-screen rendering.

The second Kind
imageView.clipsToBounds = YES;imageView.layer setCornerRadius:50];imageView.layer.shouldRasterize = YES;

Shouldrasterize=yes set rasterization allows the results of off-screen rendering to be cached in memory as bitmaps, using the cache directly, saving the performance of the off-screen rendering loss.

However, if layer and sublayers often change, it will continue to render and delete the cache re-create cache, so in this case it is recommended not to use rasterization, so it is more lossy performance.

Third: Redraw a view with rounded corners with the core graphics

This way performance is best, but UIButton do not know how to draw, you can use Uiimageview to add a click gesture as UIButton

 @implementation uiimage (circleimage)-(UIImage *) drawcircleimage { Uigraphicsbeginimagecontextwithoptions (self  .bounds.size, no, [uiscreen mainscreen].scale);   
< Span class= "hljs-variable" > [ [uibezierpath bezierpathwithroundedrect:self.bounds Cornerradius:50] addclip]; [Self Drawinrect:self.bounds]; 

Span class= "hljs-built_in" >uiimage *output = uigraphicsgetimagefromcurrentimagecontext ();
< Span class= "hljs-variable" >< Span class= "hljs-built_in" >< Span class= "hljs-variable" > Uigraphicsendimagecontext ();  
< Span class= "hljs-variable" >< Span class= "Hljs-keyword" >< Span class= "Hljs-keyword" >< Span class= "hljs-variable" >< Span class= "hljs-built_in" >  return output; 
}
@end
//在需要圆角时调用如下dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{    UIImage *img = [[UIImage imageNamed:@"image.png"] drawCircleImage]; dispatch_async(dispatch_get_main_queue(), ^{ imageView.image = img; });});

Iv. by blending layers

This method is to overlay a partially transparent view on the view where you want to add a fillet, only to obscure the fillet portion. The transparency of layer blending is handled in the opposite way as mask. Although this method is the optimal solution, there is no off-screen rendering, no additional CPU calculation, but the application scope is limited.

Summarize
    1. In scenarios where you can use a blend layer occlusion, use the fourth method first.
    2. Even non-iOS9 system, the first method in the comprehensive performance is still stronger than the latter two, more than iOS9 because there is no off-screen rendering is preferred.
    3. Method Three because of the need for a large number of calculations and add some memory, the actual choice.
 

iOS picture setting fillet performance optimization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.