Nonsense not much to say directly on the code
#import "ViewController.h"@interfaceViewcontroller () @property (nonatomic,strong) Uiimageview*ImageView;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; Self.imageview= [[Uiimageview alloc] Initwithframe:cgrectmake ( -, -, $, $)]; [Self.view AddSubview:self.imageView]; [Self yuan]; }/** * Add a circle outside the circle*/- (void) yuanhuan{//0. Loading picturesUIImage *image = [UIImage imagenamed:@"AppIcon1024"]; //the width of the pictureCGFloat IMAGEWH =Image.size.width; //set the width of the ringCGFloat border =1; //width height of large circleCGFloat OVALWH = Imagewh +2*border; //1. Turn on the bitmap contextUigraphicsbeginimagecontextwithoptions (Cgsizemake (OVALWH, OVALWH), NO,0); //2. Draw a great circleUibezierpath *path = [Uibezierpath bezierpathwithovalinrect:cgrectmake (0,0, OVALWH, OVALWH)]; [[Uicolor Redcolor]Set]; [Path fill]; //3. Set the cropping area (small circle)Uibezierpath *clippath =[Uibezierpath bezierpathwithovalinrect:cgrectmake (Border, border, IMAGEWH, IMAGEWH)]; [Clippath Addclip]; //4. Drawing pictures[Image Drawatpoint:cgpointmake (Border, border)]; //5. Get PicturesUIImage *clipimage =Uigraphicsgetimagefromcurrentimagecontext (); //6. Close the contextUigraphicsendimagecontext (); Self.imageView.image=clipimage;}/** * Crop a prototype image*/- (void) yuan{//Get PicturesUIImage *image = [UIImage imagenamed:@"AppIcon1024"]; //1. Turn on the bitmap context, just like the size of the picture//No : Opaque 0:scale not scaledUigraphicsbeginimagecontextwithoptions (Image.size, NO,0); //2. Set the graphics crop area, tangent picture//2.1 Creating a circular pathUibezierpath *path = [Uibezierpath bezierpathwithovalinrect:cgrectmake (0,0, Image.size.width, Image.size.width)]; //2.2 Set the clipping area of the path[path Addclip]; //3. Drawing pictures[Image Drawatpoint:cgpointmake (0,0)]; //4. Get the picture from the contextUIImage *clipimage =Uigraphicsgetimagefromcurrentimagecontext (); //5. Close the contextUigraphicsendimagecontext (); _imageview.image=Clipimage; }- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}@end
Source: Https://github.com/TianHero/caijian.git
iOS round picture clipping, plus a ring outside the prototype picture