+ (UIImage *) Imagedrawcirclewithimage: (UIImage *) originimage{cgfloat padding=5;//the margin of a circular image from an imageUicolor* Epsbackcolor = [Uicolor Greencolor];//background color of the imagecgsize originsize=originimage. Size; CGRect Originrect= CGRectMake (0,0, originsize. Width, originsize. height); Uigraphicsbeginimagecontext (originsize); Cgcontextref CTX=Uigraphicsgetcurrentcontext (); //The target area. CGRect Desrect= CGRectMake (padding, padding,originsize. Width-(padding*2), Originsize. Height-(padding*2 )); //sets the fill background color. Cgcontextsetfillcolorwithcolor (CTX, Epsbackcolor. Cgcolor); Uirectfill (Originrect); //the real fill//sets the ellipse deform area. Cgcontextaddellipseinrect (Ctx,desrect); Cgcontextclip (CTX); //intercepts elliptical regions. [Originimage Drawinrect:originrect];//draws the image in the target area. UIImage* Desimage =Uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext (); returnDesimage; }+ (UIImage *) Imagedrawcircleedgewithimage: (UIImage *) originimage{cgfloat padding=5;//the margin of a circular image from an imageUicolor* Epsbackcolor = [Uicolor Greencolor];//background color of the imagecgsize originsize=originimage. Size; CGRect Originrect= CGRectMake (0,0, originsize. Width, originsize. height); Uigraphicsbeginimagecontext (originsize); Cgcontextref CTX=Uigraphicsgetcurrentcontext (); //The target area. CGRect Desrect= CGRectMake (padding, padding,originsize. Width-(padding*2), Originsize. Height-(padding*2 )); //sets the fill background color. Cgcontextsetfillcolorwithcolor (CTX, Epsbackcolor. Cgcolor); Uirectfill (Originrect); //the real fill//sets the ellipse deform area. Cgcontextaddellipseinrect (Ctx,desrect); Cgcontextclip (CTX); //intercepts elliptical regions. [Originimage Drawinrect:originrect];//draws the image in the target area. //Border//cgfloat borderWidth=Ten ; Cgcontextsetstrokecolorwithcolor (CTX, [Uicolor Whitecolor]. Cgcolor); //Set Border colorCgcontextsetlinecap (CTX, Kcglinecapbutt); Cgcontextsetlinewidth (CTX, borderWidth); //sets the border width. Cgcontextaddellipseinrect (CTX, desrect);//Draw a circle in this boxCgcontextstrokepath (CTX);//The stroke border. //Border//UIImage* Desimage = Uigraphicsgetimagefromcurrentimagecontext ();//gets the image in the context of the current drawing. Uigraphicsendimagecontext (); returndesimage;}
How iOS makes an avatar picture a circle