How iOS cuts round corners

Source: Internet
Author: User

How iOS cuts round corners

The first method: by setting the properties of the layer

The simplest one, but it affects performance, and is rarely used in normal development.

     uiimageview *imageview = [[uiimageview alloc]initWithFrame : cgrectmake (100, 100, 100, 100)]; //only need to set the Layer two properties //set fillet ImageView .layer.cornerradius = ImageView .frame.size.width/2; //to cut out the extra parts Imageview.layeryes; [self.view Addsubview:imageview];      
Second method: Use Bezier Uibezierpath and the core graphics frame to draw a fillet
     Uiimageview *imageview = [[Uiimageview Alloc]initwithframe:CGRectMake (100,100,100,100)]; ImageView. image = [UIImage imagenamed:@ "1"];Start drawing on ImageViewuigraphicsbeginimagecontextwithoptions (Imageview.size, no, 1.0); //use Bezier curves to draw a circular chart [[uibezierpath bezierpathwithroundedrect: Imageview.bounds cornerradius:imageview.frame.size.width] addclip]; [ImageView drawrect:imageview.bounds]; Imageview.image = Span class= "hljs-built_in" >uigraphicsgetimagefromcurrentimagecontext (); //end drawing uigraphicsendimagecontext (); [self.view Addsubview:imageview];     
Third method: Use Cashapelayer and uibezierpath to set rounded corners

First you need to import<AVFoundation/AVFoundation.h>

#import"ViewController.h"@interfaceViewcontroller ()@end@implementationviewcontroller-(void) Viewdidload {[Super Viewdidload];Uiimageview *imageview = [[Uiimageview Alloc]initwithframe:CGRectMake (100,100,100,100)]; ImageView. image = [UIImage imagenamed:@ "1"];uibezierpath *maskpath = [uibezierpath Bezierpathwithroundedrect:imageview.bounds byroundingcorners: uirectcornerallcorners cornerradii:imageview.bounds< Span class= "hljs-variable" >.size]; cashapelayer *masklayer = [[cashapelayer Alloc]init]; //set size Masklayer.frame = Imageview//set graphic look masklayer.path = Maskpath .layer.mask = Masklayer; [self.view Addsubview:imageview];}     

The third of these three methods is best, the memory consumption is the least Ah, and rendering fast.



Wen/ffib (author of Jane's book)
Original link: http://www.jianshu.com/p/e97348f42276
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".

How iOS cuts round corners

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.