Draw a dashed line, this method can be written into the classification of UIImage, directly written as a class method call.
1-(UIImage *) Drawdashimagewithframe: (CGRect) frame andlinecolor: (Uicolor *) LineColor2 {3 //turn on the graphics context4 Uigraphicsbeginimagecontext (frame.size);5 6 //Get Context7Cgcontextref Cxtref =Uigraphicsgetcurrentcontext ();8 9 //set the thread head styleTen Cgcontextsetlinecap (Cxtref, kcglinecapround); One A //Draw 3 points, skip 2 points, draw 3 points, skip 3 points, draw 2 points, so repeat - //cgfloat lengths[] = {3,2,3}; - //6 is the length of each dashed line, and 5 is the interval between dashes theCGFloat lengths[] = {6,5}; - - //Set line width (extend to both sides) -Cgcontextsetlinewidth (Cxtref,1); + - //set line Rendering color + [LineColor Setstroke]; A at //draw a dashed line - //Parameter 2: How many points to skip when drawing first - //parameter 3: Array - //parameter 4: array length -Cgcontextsetlinedash (Cxtref,0, lengths,2); - in //start Drawing line-Starting point -Cgcontextmovetopoint (Cxtref,0.0,0.0); to + //Harness Draw Line-end point -Cgcontextaddlinetopoint (Cxtref, [UIScreen mainscreen].bounds.size.width,0.0); the * //Set Strokes $ Cgcontextstrokepath (cxtref);Panax Notoginseng - //get a picture from the graphics context theUIImage *img =Uigraphicsgetimagefromcurrentimagecontext (); + A //Close the graphics context the Uigraphicsendimagecontext (); + - returnimg; $}
Draw a dashed line