This article reprinted to http://blog.csdn.net/weisubao/article/details/41282457
[OBJC]View Plaincopy
- -(void) DrawRect: (cgrect) rect {
- //Get current context
- Cgcontextref Ctx=uigraphicsgetcurrentcontext ();
- //Save the current context state in the stack
- Cgcontextsavegstate (CTX);
- //Zoom, move processing (need to be set before drawing)
- CGCONTEXTSCALECTM (CTX, 0. 5, 0. 5);
- CGCONTEXTTRANSLATECTM (CTX, 100, 100);
- CGCONTEXTROTATECTM (CTX, M_pi_4);
- //Stroke
- Cgcontextmovetopoint (CTX, 10, 10);
- Cgcontextaddlinetopoint (CTX, 100, 100);
- Cgcontextaddlinetopoint (CTX, 150, 50);
- two ways to close the loop
- //cgcontextaddlinetopoint (CTX, 10, 10);
- Cgcontextclosepath (CTX);
- //Render drawing, solid and hollow
- Cgcontextstrokepath (CTX);
- //cgcontextfillpath (CTX);
- //Save the current context state in the stack
- Cgcontextsavegstate (CTX);
- //Draw a square
- Cgcontextaddrect (CTX, CGRectMake (100, 100, 50, 50));
- //Set the line width (must be before Cgcontextstrokepath)
- //Because there was a previous rendering of the drawing, so this property setting does not affect the above triangle, the following color settings similarly
- ///So, if you want to set the properties of two or more shapes separately, render the drawing one at a time
- Cgcontextsetlinewidth (CTX, 10);
- //Set color (in the same vein, the code for the property setting is preceded by the drawing's code)
- Cgcontextsetrgbstrokecolor (CTX, 1, 0, 0, 1);
- Cgcontextstrokepath (CTX);
- //Set style
- Cgcontextmovetopoint (CTX, 20, 160);
- Cgcontextaddlinetopoint (CTX, 200, 280);
- Cgcontextaddlinetopoint (CTX, 250, 200);
- Cgcontextsetlinewidth (CTX, 20);
- //Set the Kinsoku style
- Cgcontextsetlinecap (CTX, Kcglinecapround);
- //Set the corner style
- Cgcontextsetlinejoin (CTX, Kcglinejoinround);
- Cgcontextstrokepath (CTX);
- //Remove the context state stored in the stack and restore it. The above code setting style does not affect the other
- Cgcontextrestoregstate (CTX);
- //Draw Ellipse
- Cgcontextaddellipseinrect (CTX, CGRectMake (200, 130, 60, 30));
- //below equivalent
- //cgcontextstrokepath (CTX);
- Cgcontextdrawpath (CTX, Kcgpathstroke);
- //Draw round
- Cgcontextaddellipseinrect (CTX, CGRectMake (140, 170, 50, 50));
- Cgcontextsetlinewidth (CTX, 3);
- Cgcontextstrokepath (CTX);
- //Draw arc
- Cgcontextaddarc (CTX, 200, 50, 50, M_pi_4, M_PI, 1);
- Cgcontextstrokepath (CTX);
- //Draw 1/4 circles, and a new way to set the color
- Cgcontextmovetopoint (CTX, 10, 230);
- Cgcontextaddlinetopoint (CTX, 10, 280);
- Cgcontextaddlinetopoint (CTX, 60, 280);
- Cgcontextaddarc (CTX, 10, 280, 50, 0,-m_pi_2, 1);
- [[Uicolor Greencolor] Setstroke];
- Cgcontextstrokepath (CTX);
- //Draw pictures and text (do not need to get context manually)
- nsstring *str1=@ "Xin-chou-year-empty work";
- [str1 drawatpoint:cgpointzero withattributes: nil];
- UIImage *img=[uiimage imagenamed:@ "001"];
- [img drawatpoint:cgpointmake (10, 10)];
- //overlap pictures and sign in one box
- CGRect rect1=cgrectmake (50, 50, 10 0, 10 0);
- [img drawaspatterninrect:rect1];
- nsmutabledictionary *attr=[[nsmutabledictionary Alloc]init];
- Attr[nsforegroundcolorattributename]=[uicolor Whitecolor];
- Attr[nsfontattributename]=[uifont systemfontofsize:13];
- [str1 drawinrect:cgrectmake (50, 140, 10 0, 10 0) WITHATTRIBUTES:ATTR];
- //Remove the context state stored in the stack and restore it. The above code setting style does not affect the other
- Cgcontextrestoregstate (CTX);
- //Cut round head
- Cgcontextaddellipseinrect (CTX, CGRectMake (150, 150, 10 0, 100)) ;
- //Cut out a context area by a circle, and the later content is filled in the circular context
- Cgcontextclip (CTX);
- UIImage *img1=[uiimage imagenamed:@ "Me"];
- [img1 drawatpoint:cgpointmake (150, 150)];
- }
"iOS Dev-80" quartz2d Drawing Introduction: Line/Circle/ellipse/square and context stack management Cgcontextsavegstate/cgcontextrestoregstate