Package: Generate Avatar (UIImage (NJ). h/. M
@interface UIImage (NJ) /* * * Generate avatar * * @param icon avatar picture name * @param border avatar Border size * @param color of the picture border * * c11>*/+ (instancetype) Imagewithicon: (NSString *) icon border: (nsinteger) Border color: (Uicolor *) color; @end
View Code
@implementationUIImage (NJ)+ (Instancetype) Imagewithicon: (NSString *) icon border: (nsinteger) Border color: (Uicolor *) color{//0. Load the original imageUIImage *image =[UIImage Imagenamed:icon]; //1. Create a picture contextCGFloat margin =border; Cgsize size= Cgsizemake (image.size.width + margin, Image.size.height +margin); //YES Opaque no transparentUigraphicsbeginimagecontextwithoptions (Size, NO,0); //2. Draw a great circleCgcontextref CTX =Uigraphicsgetcurrentcontext (); Cgcontextaddellipseinrect (CTX, CGRectMake (0,0, Size.width, size.height)); [ColorSet]; Cgcontextfillpath (CTX); //3. Draw a small CircleCGFloat smallx = margin *0.5; CGFloat smally= Margin *0.5; CGFloat SMALLW=Image.size.width; CGFloat SMALLH=Image.size.height; Cgcontextaddellipseinrect (CTX, CGRectMake (Smallx, smally, SMALLW, SMALLH)); //[[Uicolor Greencolor] set]; //Cgcontextfillpath (CTX); //4. The scope of the guidance available, the scope of application of the scope is after the designation, also said in the range of the specified cut before the thing is not affectedCgcontextclip (CTX); //5. Drawing pictures[Image Drawinrect:cgrectmake (Smallx, smally, SMALLW, SMALLH)]; //6. Remove the imageUIImage *newimage =Uigraphicsgetimagefromcurrentimagecontext (); returnnewimage;}View Code
Call
@property (Weak, nonatomic) iboutlet Uiimageview *IV; @end @implementation Njviewcontroller-(void) viewdidload{ [Super Viewdidload]; Imagewithicon:@ "me" border: color:[uicolor Greencolor]] ; = newimage; }
IOS Picture clipping (encapsulating data)