- In iOS development, we often encounter the problem of setting rounded corners, here are a few ways to set the fillet:
The first method: by setting the property code for the layer:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"willwang"]];//只需要设置layer层的两个属性//设置圆角imageView.layer.cornerRadius =50//将多余的部分切掉imageView.layer.masksToBounds = YES;[self.view addSubview:imageView];
- The implementation of Masktobounds will trigger off-screen rendering (offscreen rendering), the GPU in the current screen buffer outside the new rendering buffer to work, that is, off-screen rendering, which will give us additional performance loss, If such a fillet operation reaches a certain number, it will trigger frequent merging of buffers and frequent switching of the context, the cost of performance will be the macro surface now the user experience on < off frame > is not recommended.
The second method: Use the Bezier Uibezierpath and the core graphics frame to draw a fillet:
Code:
Uiimageview *imageview = [[Uiimageview Alloc]initwithframe:cgrectmake (100,100,100,100)]; ImageView. image = [UIImage imagenamed:@ "Willwang"]; //began to paint ImageView uigraphicsbeginimagecontextwithoptions (imageView .bounds.size, NO, 0.0); //using Bezier curves to draw a circular chart [[Uibezierpath Bezierpathwithroundedrect:imageview.bounds cornerradius:imageview.frame.width] addclip]; [ImageView drawrect:imageview.bounds]; Imageview.image = Uigraphicsgetimagefromcurrentimagecontext (); //end drawing Uigraphicsendimagecontext (); [self.view Addsubview:imageview];
- Uigraphicsbeginimagecontextwithoptions (cgsize size, BOOL opaque, cgfloat scale) each parameter means:
- size--the size of the newly created bitmap context
- opaque--Transparent switch, if the graphic is not transparent at all, set to Yes to optimize the storage of the bitmap.
- scale--Zoom Factor IPhone 4 is 2.0, the other is 1.0. Although this can be obtained using [UIScreen Mainscreen].scale, the system will automatically set the correct scale after it is actually set to 0.
Third method: Use the core graphics frame to draw a fillet code:
Uiimageview *imageview = [[Uiimageview Alloc]initwithframe:cgrectmake (100,200,100,100)]; ImageView. image = [UIImage imagenamed:@ "Willwang"];Start Drawing uigraphicsbeginimagecontextwithoptions for ImageView (ImageView. Bounds. Size,NO,1.0);Get the graphics context cgcontextref CTX = Uigraphicsgetcurrentcontext ();Set a rangeCGRect rect = CGRectMake (0, 0, Imageview.frame.size.width, Imageview.frame.size.height); //creates an ellipse cgcontextaddellipseinrect (CTX, Rect) based on a rect; //clipping cgcontextclip (CTX); //to draw the original photo to the graphics context [Imageview.image Drawinrect:rect]; //get the cropped photo from the context uiimage *newimage = Uigraphicsgetimagefromcurrentimagecontext (); //Close context Uigraphicsendimagecontext (); Imageview.image = NewImage; [self.view Addsubview:imageview];
Fourth method: Use Cashapelayer and Uibezierpath to set the fillet code:
Uiimageview *imageview = [[Uiimageview Alloc]initwithframe:cgrectmake (100,100,100,100)]; ImageView. image = [UIImage imagenamed:@ "Willwang"]; Uibezierpath *maskpath = [Uibezierpath bezierpathwithroundedrect:imageview. Bounds byroundingcorners: Uirectcornerallcorners cornerradii:imageview. Bounds. Size]; Cashapelayer *masklayer = [[Cashapelayer alloc]init]; //Set size Masklayer. frame = ImageView. Bounds; //Set the graphics to look masklayer. Path = Maskpath. Cgpath; ImageView. Layer. Mask = Masklayer; [self. View Addsubview:imageview];
The fourth method extends to specify the angle method that needs to be rounded:
+ (UIBezierPath *)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii
- The corners parameter specifies the corner to be rounded, and the enumeration type is as follows:
typedef NS_OPTIONS(NSUInteger, UIRectCorner) { UIRectCornerTopLeft 1 << 0, UIRectCornerTopRight = 1 << 1, UIRectCornerBottomLeft = 1 << 2, UIRectCornerBottomRight = 1 << 3, UIRectCornerAllCorners = ~0UL};
:
Code:
Set the view location and sizeUIView *myview = [[UIView Alloc] Initwithframe:cgrectmake (120,300,100,50)];Set the background color MyView. backgroundcolor = [Uicolor Redcolor];AddSelf. View Addsubview:myview];Draw fillet corners to set using the | To combine Uibezierpath *maskpath = [Uibezierpath bezierpathwithroundedrect:myview. Bounds Byroundingcorners:uirectcornertopleft | Uirectcornerbottomright Cornerradii:cgsizemake (20,20)]; Cashapelayer *masklayer = [[Cashapelayer alloc] init];Set Size Masklayer.frame = Myview.bounds; //set graphic look masklayer.path = Maskpath .layer.mask = Masklayer; uilabel *label = [[uilabel Alloc]init]; //Add text Label.text = @" Willwang "; //text color Label.textcolor = [UIColor WhiteColor ]; [MyView Addsubview:label]; //automatic layout [Label mas_makeconstraints:^ (Masconstraintmaker *make) {Make.center.equalto (MyView);}];
The fifth method is set in storyboard or Xib
How iOS sets rounded corners and specifies the position of rounded corners