ios-border picture, Avatar border

Source: Internet
Author: User

Occasionally there is a need for a framed picture in the project, or the avatar or logo. Add a custom color border to the outside of the prototype picture, the idea is to draw according to the context, write the demo and explain.
Picture with frame Demo:

-(void) borderimage{//1. Loading pictures    UIImage*image = [UIImageimagenamed:@"Baby"];//2. Border Width    CGFloatBORDERW =Ten;//3. Opening the picture context    cgsizeSize = Cgsizemake (image. Size. Width+2*BORDERW, image. Size. Height+2*BORDERW); Uigraphicsbeginimagecontextwithoptions (Size,NO,0);//4. First describe a great circle as a fillUibezierpath *path = [Uibezierpath bezierpathwithovalinrect:cgrectmake (0,0, size. Width, size. Height)]; [[UicolorRedcolor] set]; [Path fill];//5. Add a small circle to the clipping areaPath = [Uibezierpath bezierpathwithovalinrect:cgrectmake (Borderw, borderw, image. Size. Width, image. Size. Height)]; [Path Addclip];//6. Drawing the picture context[Image Drawinrect:cgrectmake (Borderw, borderw, image. Size. Width, image. Size. Height)];//7. Creating a picture    UIImage*newimage = Uigraphicsgetimagefromcurrentimagecontext ();//8. Closing the contextUigraphicsendimagecontext (); Self. Imagev. Image= NewImage;}

Easy-to-call classification of UIIAMGE:

/** * Picture with frame * * @param imageName picture name * @param imagewidth picture width * @param imageheight Picture Height * @param borderWidth Border Width * @param bordercolor Border Color * * @return picture with frame */+ (UIImage *) Imagewithimagename: (NSString *) imageName imagewidth: (cgfloat) imagewidth imageheight: (cgfloat) ImageHeight borderWidth: (cgfloat) borderWidth bordercolor: (Uicolor *) bordercolor{UIImage *image = [UIImage imagenamed:    ImageName]; Cgsize size = Cgsizemake (ImageWidth +2* BorderWidth, ImageHeight +2* borderWidth); Uigraphicsbeginimagecontextwithoptions (Size, NO,0); Uibezierpath *path = [Uibezierpath bezierpathwithovalinrect:cgrectmake (0,0, Size.width, Size.Height)];    [BorderColor set];    [Path fill];    Path = [Uibezierpath bezierpathwithovalinrect:cgrectmake (BorderWidth, BorderWidth, ImageWidth, ImageHeight)];    [Path Addclip];    [Image Drawinrect:cgrectmake (BorderWidth, BorderWidth, ImageWidth, ImageHeight)];    UIImage *newimage = Uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext ();returnNewImage;}

Full Project Demo Link:
Https://github.com/qxuewei/XWBorderImage

ios-border picture, Avatar border

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.