First approach: The most common approach, but with the worst performance
Uiimageview *imageview = [[Uiimageview alloc]initwithframe:cgrectmake (100, 100, 100, 100)];
Imageview.image = [UIImage imagenamed:@ "late ant. jpg"];
You only need to set the layer's two properties
Set rounded corners
ImageView.layer.cornerRadius = IMAGEVIEW.FRAME.SIZE.WIDTH/2;
Cut out the extra parts
ImageView.layer.masksToBounds = YES;
[Self.view Addsubview:imageview];
The second method: using Bezier Uibezierpath and the core graphics frame to draw a rounded corner, the performance is much higher than the first (some people say 100 times, specifically how much I do not know)
Uiimageview *imageview = [[Uiimageview alloc]initwithframe:cgrectmake (100, 100, 100, 100)];
Imageview.image = [UIImage imagenamed:@ "late ant. jpg"];
Start drawing on ImageView
Uigraphicsbeginimagecontextwithoptions (ImageView.bounds.size, NO, 1.0);
Draw a circular chart using Bezier curves
[[Uibezierpath BezierPathWithRoundedRect:imageView.bounds cornerRadius:imageView.frame.size.width] addclip];
[ImageView DrawRect:imageView.bounds];
Imageview.image = Uigraphicsgetimagefromcurrentimagecontext ();
Finish drawing
Uigraphicsendimagecontext ();
[Self.view Addsubview:imageview];
Third method: Use Cashapelayer and uibezierpath to set rounded corners
First you need to import <AVFoundation/AVFoundation.h>
The memory consumption is minimal, and the rendering is fast.
Uiimageview *imageview = [[Uiimageview alloc]initwithframe:cgrectmake (100, 100, 100, 100)];
Imageview.image = [UIImage imagenamed:@ "late ant. jpg"];
Uibezierpath *maskpath = [Uibezierpath bezierPathWithRoundedRect:imageView.bounds byroundingcorners: Uirectcornerallcorners CornerRadii:imageView.bounds.size];
Cashapelayer *masklayer = [[Cashapelayer alloc]init];
Set size
Masklayer.frame = Imageview.bounds;
Set graphic appearance
Masklayer.path = Maskpath.cgpath;
ImageView.layer.mask = Masklayer;
[Self.view Addsubview:imageview];
Common ways to set rounded corners in iOS