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