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