Rounded corners Optimization

Source: Internet
Author: User

Ingenious application of self-observation

Since you want to create a rounded picture, the first thing to do is to solve the generation timing problem. May have a friend think of Swizzle class Uiimageview SetImage method, but I personally do not recommend, after all, Swizzle class method influence scope is too wide, for large development team, after the problem is difficult to troubleshoot location problem. Define Uiimageview subclasses? Practicality is not strong!

Remember my three ways to implement self-release as mentioned in the article "self-release" in iOS development? One of these is the dynamic attribute observer--the purpose of self-monitoring by creating a dynamic property that kvo a property of the object being observed.

By creating the dynamic property observer Hjimageobserver, listening to the image property of the Uiimageview, when the image changes, can immediately trigger the creation of a rounded picture, so as to achieve the goal of automatic fillet picture replacement, the specific implementation can refer to the source code Hjcornerradius

Proper switching of Runloop

A careful friend may notice that this tool does not directly render the original image object, but rather intercepts the image generated by the Uiimageview view layer before rendering. The reason is simple because the Uiimageview rendering approach involves a variety of contentmode, and the image generated by rendering the Uiimageview view layer can subtly solve the problem of Uiimageview display mode

In addition, for system reasons, for a uiimageview like UITableViewCell, the first time you create an assignment, you may not be able to get a picture of the Uiimageview view layer, at which point the asynchronous runloop can be toggled to achieve the purpose of time-lapse rendering

It is well known that the logic of rendering can be run on the worker thread, but this tool does not put them on the worker thread because there is a delay in the work thread, which causes the picture to blink.

The specific rendering code is as follows

<textarea class= "Crayon-plain print-no" style= "-moz-tab-size:4"; Font-size:12px! Important Line-height:17px! Important z-index:0; opacity:0; Overflow:hidden; "ReadOnly data-settings=" DblClick ">uigraphicsbeginimagecontextwithoptions ( Self.originImageView.bounds.size, NO, [UIScreen Mainscreen].scale); Cgcontextref Currnetcontext = Uigraphicsgetcurrentcontext (); Cgcontextaddpath (Currnetcontext, [Uibezierpath bezierPathWithRoundedRect:self.originImageView.bounds Cornerradius : Self.cornerradius]. Cgpath); Cgcontextclip (Currnetcontext); [Self.originImageView.layer Renderincontext:currnetcontext]; UIImage *image = Uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext (); if ([Image Iskindofclass:[uiimage class]]) {Image.alicornerradius = YES; Self.originImageView.image = image; } else {Dispatch_async (Dispatch_get_main_queue (), ^{[self updateimageview]; }); }</textarea>
12345678910111213141516 Uigraphicsbeginimagecontextwithoptions (Self.originImageView.bounds.size, NO, [UIScreen mainscreen].scale);     cgcontextref Currnetcontext = Uigraphicsgetcurrentcontext ();     Cgcontextaddpath (Currnetcontext, [Uibezierpath bezierPathWithRoundedRect:self.originImageView.bounds Cornerradius : Self.cornerradius]. Cgpath);     cgcontextclip (currnetcontext);     [ Self.originImageView.layer Renderincontext:currnetcontext];    uiimage *image = Uigraphicsgetimagefromcurrentimagecontext ();     uigraphicsendimagecontext ();         if ([Image Iskindofclass:[uiimage class]]) {         image.alicornerradius = yes;         Self.originImageView.image = image;    } else {         dispatch_async (Dispatch_get_main_queue (), ^{            [self updateImageView];         });     }

The actual test verifies that the effect is still relatively ideal, even if a large number of rounded pictures are needed, the display frame number can be stabilized at more than 50 frames. Less impact on CPU consumption, IPhone6 test shows that the actual CPU consumption rise will not exceed 5%, within reasonable range

Rounded corners 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.