IOS Draw a circle head

Source: Internet
Author: User


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;

}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.