Through the path we can achieve more complex drawing, such as polygons, arcs, rounded rectangles and so on
- (void) DrawRect: (cgrect) rect{//Get Image Context ObjectCgcontextref context =Uigraphicsgetcurrentcontext (); Cgcontextsetrgbstrokecolor (Context, -/255.0, -/255.0, -/255.0,1); Cgcontextsetfillcolorwithcolor (context, [Uicolor Greencolor]. Cgcolor); Cgcontextsetshadowwithcolor (Context, Cgsizemake (1,1),3, [Uicolor Yellowcolor]. Cgcolor); Cgcontextsetlinewidth (Context,2); //Add a line (Mode 1)//1, 2 points constitute the first segment, 2, 3 points constitute the second line segment, 3, 4 points constitute the 3rd segment, and so onCgpoint points[] = {Cgpointmake ( -, the), Cgpointmake ( Max, the), Cgpointmake ( -, -), Cgpointmake ( -, -), Cgpointmake ( $, -)}; Cgcontextaddlines (context, points,5); Cgcontextdrawpath (context, kcgpathstroke); //Add a line (Mode 2)Cgcontextmovetopoint (Context,0, +); Cgcontextaddlinetopoint (Context, -, +); Cgcontextaddlinetopoint (Context, -, -); //close the path, connect the end point and start pointCgcontextclosepath (context); Cgcontextdrawpath (context, kcgpathstroke); //Add a rectangleCgcontextaddrect (Context, CGRectMake ( Max, -, -, -)); Cgcontextdrawpath (context, Kcgpathfill); //Add EllipseCgcontextaddellipseinrect (Context, CGRectMake (0, -, -, -)); Cgcontextdrawpath (context, kcgpathstroke); //Add Arc//the last parameter represents the direction, 0 indicates clockwise, and 1 is counterclockwiseCgcontextaddarc (Context, -, Max, -, - -*m_pi/ the, -*m_pi/ the,1); Cgcontextdrawpath (context, kcgpathstroke); //add a two-time function curveCgcontextmovetopoint (Context, -, $); Cgcontextaddquadcurvetopoint (Context, the, -, the, -); Cgcontextdrawpath (context, kcgpathstroke); //Add Bezier curvesCgcontextmovetopoint (Context,Ten, $); Cgcontextaddcurvetopoint (Context, the, the,0, -, -, -); Cgcontextdrawpath (context, kcgpathstroke); }
Run results
It can be seen from the above example that the use of the path can be easily drawn based on the geometry and arc, by combining these geometries can be more complex images, the following code to achieve a rounded rectangle, polygon drawing
//Draw rounded rectangles//rect represents the rectangular area, radius represents the fillet radiusvoiddrawroundrect (cgcontextref context,cgrect rect,cgfloat radius) {Cgpoint origin=Rect.origin; Cgsize size=rect.size; Cgcontextmovetopoint (context, origin.x+radius, ORIGIN.Y); //Add TopCgcontextaddlinetopoint (context, origin.x + size.width-radius, ORIGIN.Y); //add an arc in the upper right cornerCgcontextaddarctopoint (context, origin.x + size.width, ORIGIN.Y, origin.x + size.width, ORIGIN.Y +radius, radius); //Add RightCgcontextaddlinetopoint (context, origin.x + size.width, ORIGIN.Y + size.height-radius); //add arc in lower right cornerCgcontextaddarctopoint (context, origin.x + size.width, ORIGIN.Y + size.height, origin.x + Size.width-radius, ORIGIN.Y +size.height, RADIUS); //Add BottomCgcontextaddlinetopoint (Context, origin.x + radius, ORIGIN.Y +size.height); //add an arc in the lower left cornerCgcontextaddarctopoint (context, origin.x, ORIGIN.Y + size.height, origin.x, ORIGIN.Y + size.height-radius, radius); //Add leftCgcontextaddlinetopoint (context, origin.x, ORIGIN.Y +radius); //add an arc to the upper left cornerCgcontextaddarctopoint (context, origin.x, ORIGIN.Y, origin.x +radius, ORIGIN.Y, radius); }- (void) DrawRect: (cgrect) rect{//Get Image Context ObjectCgcontextref context =Uigraphicsgetcurrentcontext (); Cgcontextsetrgbstrokecolor (Context,1,1,1,0.7); Cgcontextsetfillcolorwithcolor (context, [Uicolor Greencolor]. Cgcolor); Cgcontextsetlinewidth (Context,2); Drawroundrect (Context,cgrectmake ( -, -, $, Max), -); Cgcontextdrawpath (context, Kcgpathstroke);}
Run results
//draw a multi-angle shape//CenterPoint represents the center point, size represents the length of the edge, and count represents the number of cornersvoidDrawmulangle (cgcontextref context,cgpoint centerpoint,cgfloat size,intcount) {CGFloat dig=4* M_PI/count; Cgcontextmovetopoint (context, centerpoint.x, Centerpoint.y+size); for(inti = 1; I <= count; i++) {cgfloat x= sin (i*dig) * size +Centerpoint.x; CGFloat y= cos (i*dig) * size +Centerpoint.y; Cgcontextaddlinetopoint (context, x, y); }}- (void) DrawRect: (cgrect) rect{//Get Image Context ObjectCgcontextref context =Uigraphicsgetcurrentcontext (); Cgcontextsetrgbstrokecolor (Context,1,1,1,0.7); Cgcontextsetfillcolorwithcolor (context, [Uicolor Greencolor]. Cgcolor); Cgcontextsetlinewidth (Context,2); Cgcontextbeginpath (context); Drawmulangle (Context,cgpointmake ( the, $), -,5); Cgcontextclosepath (context); Cgcontextdrawpath (context, Kcgpathfillstroke);}
Run results
iOS image processing (3) Drawing paths