1. Create class inheritance UIImage;
2. Create a method for plotting dashed lines:
+ (ID) imagewithsize: (cgsize) Size bordercolor: (Uicolor *) color borderWidth: (cgfloat) BorderWidth
{
Open Picture Context
Uigraphicsbeginimagecontextwithoptions (Size, NO, 0.0);
Set color
[[Uicolor Clearcolor] set];
Get Current context
Cgcontextref context = Uigraphicsgetcurrentcontext ();
Cgcontextbeginpath (context);
Setting properties
Cgcontextsetlinewidth (context, borderWidth);
Cgcontextsetstrokecolorwithcolor (context, color. Cgcolor);
CGFloat lengths[] = {3, 1};
Cgcontextsetlinedash (context, 0, lengths, 1);
Cgcontextmovetopoint (context, 0.0, 0.0);
Cgcontextaddlinetopoint (context, size.width, 0.0);
Cgcontextaddlinetopoint (context, size.width, size.height);
Cgcontextaddlinetopoint (context, 0, size.height);
Cgcontextaddlinetopoint (context, 0.0, 0.0);
Draw
Cgcontextstrokepath (context);
Gets the context of the drawn picture
uiimage* image = Uigraphicsgetimagefromcurrentimagecontext ();
Close Picture Context
Uigraphicsendimagecontext ();
return image;
}
3. External calls. Remember: To call with a class method (good carelessness ...). At first do not use the class method call, resulting in the case of stuck in the method can not be adjusted ....)
-(void) Viewdidload {
[Super Viewdidload];
Implementing a dashed border class method call
UIImage *img = [Creatimage imagewithsize:cgsizemake (a) Bordercolor:[uicolor Redcolor] borderwidth:3];
Uiimageview *IMG1 = [[Uiimageview alloc]initwithframe:cgrectmake (100, 100, 100, 100)];
[Img1 setimage:img];
[Self.view ADDSUBVIEW:IMG1];
}
I also according to other people's blog practice to do, learn to learn. Oh.....
Implementation of dashed borders