Demo:http://pan.baidu.com/s/1mgbf6yg
_logoimageview . Image = [self getellipseimagewithimage: [UIImageimagenamed :@ "1_wsq724439564.jpg"];
-(UIImage*) Getellipseimagewithimage: (UIImage*) originimage
{
cgfloat padding = 5; // the margin of a circular image from an image
uicolor* epsbackcolor = [uicolorgreencolor]; // background color of the image
cgsize originsize = originimage. size;
cgrect originrect = cgrectmake(0, 0, originsize. Width, originsize. height);
uigraphicsbeginimagecontext(originsize);
cgcontextref ctx =uigraphicsgetcurrentcontext();
// target area.
cgrect desrect = cgrectmake(padding, padding,originsize. Width-(padding*2), Originsize. Height-(padding*2));
// set fill background color.
cgcontextsetfillcolorwithcolor(CTX, Epsbackcolor. Cgcolor );
Uirectfill (Originrect); // the real fill
// set 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();
return desimage;
}
A rounded head with a border.
-(UIImage*) Getellipseimagewithimage: (UIImage*) originimage
{
cgfloat padding = 5; // the margin of a circular image from an image
uicolor* epsbackcolor = [uicolorgreencolor]; // background color of the image
cgsize originsize = originimage. size;
cgrect originrect = cgrectmake(0, 0, originsize. Width, originsize. height);
uigraphicsbeginimagecontext(originsize);
cgcontextref ctx =uigraphicsgetcurrentcontext();
// target area.
cgrect desrect = cgrectmake(padding, padding,originsize. Width-(padding*2), Originsize. Height-(padding*2));
// set fill background color.
cgcontextsetfillcolorwithcolor(CTX, Epsbackcolor. Cgcolor );
Uirectfill (Originrect); // the real fill
// set 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, [uicolorwhitecolor]. Cgcolor ); // Set Border color
cgcontextsetlinecap(CTX,Kcglinecapbutt);
Cgcontextsetlinewidth (CTX, BorderWidth); // sets the border width.
cgcontextaddellipseinrect(CTX, desrect); // Draw a circle in this box
Cgcontextstrokepath(CTX); // The stroke border.
// border //
UIImage* desimage =uigraphicsgetimagefromcurrentimagecontext(); // gets the image in the context of the current drawing.
uigraphicsendimagecontext();
return desimage;
}