/*
-(ID) Initwithcoder: (Nscoder *) Adecoder
{
if (self = [Super Initwithcoder:adecoder]) {
set the main layer fillet of a picture
Self.iconView.layer.cornerRadius = 8;
set partial clipping beyond the main layer
Self.iconView.clipsToBounds = YES;
Self.iconView.layer.masksToBounds = YES;
NSLog (@ "Initwithcoder");
}
return self;
}
*/
-(void) awakefromnib
{
// set the main layer fillet of the picture
self. IconView. Layer. Cornerradius = 8;
// Set partial clipping beyond the main layer
self. IconView. Clipstobounds = YES;
}
#import "Uiimage+dy.h" @implementation UIImage (DY) + (Instancetype) Circleimagewithname: (NSString *) name BorderWidth: ( CGFloat) borderWidth bordercolor: (Uicolor *) bordercolor{//1. Loading the original image UIImage *oldimage = [UIImage imagenamed:name]; 2. Open context CGFloat Imagew = oldImage.size.width + 2 * borderWidth; CGFloat Imageh = oldImage.size.height + 2 * borderWidth; Cgsize imageSize = Cgsizemake (Imagew, Imageh); Uigraphicsbeginimagecontextwithoptions (ImageSize, NO, 0.0); 3. Get the current context cgcontextref CTX = Uigraphicsgetcurrentcontext (); 4. Draw the frame (great circle) [BorderColor set]; CGFloat Bigradius = Imagew * 0.5; Circle radius cgfloat CenterX = Bigradius; Center CGFloat centery = Bigradius; Cgcontextaddarc (CTX, CenterX, CenterY, Bigradius, 0, M_PI * 2, 0); Cgcontextfillpath (CTX); Draw Circle//5. Small round cgfloat Smallradius = bigradius-borderwidth; Cgcontextaddarc (CTX, CenterX, CenterY, Smallradius, 0, M_PI * 2, 0); Cropping (the things that are drawn later are affected by clipping) Cgcontextclip (CTx); 6. Drawing [Oldimage Drawinrect:cgrectmake (BorderWidth, BorderWidth, OldImage.size.width, OldImage.size.height)]; 7. Take figure UIImage *newimage = Uigraphicsgetimagefromcurrentimagecontext (); 8. End context Uigraphicsendimagecontext (); return newimage;}
iOS picture clipping