1. Line
1. Get the graphics context
Cgcontextref CTX = Uigraphicsgetcurrentcontext ();
Set line width
Cgcontextsetlinewidth (CTX, 5);
Set color
Cgcontextsetrgbstrokecolor (CTX, 0, 0, 1, 1);
Set head end style
Cgcontextsetlinecap (CTX, Kcglinecapround);
Set the style of the inflection point
Cgcontextsetlinejoin (CTX, Kcglinejoinround);
2. Setting the starting point
Cgcontextmovetopoint (CTX, 10, 10);
3. Add a line segment
Cgcontextaddlinetopoint (CTX, 100, 100);
Add a line segment again
Cgcontextaddlinetopoint (CTX, 50, 100);
4. Rendering
Cgcontextstrokepath (CTX);
Note: The use method can be generalized to get context → set context properties → Set starting point → add segment → render
There are 3 types of head and tail styles: Kcglinecapbutt (left), Kcglinecapround (right), Kcglinecapsquare (left), butt and square are the same
There are 3 types of turning points:Kcglinejoinbevel (left), Kcglinejoinround (middle), Kcglinejoinmiter (right)
2. Triangles
Get context
Cgcontextref CTX = Uigraphicsgetcurrentcontext ();
//Starting point
Cgcontextmovetopoint (CTX, 0, 0);
//Add a line
Cgcontextaddlinetopoint (CTX, 100, 100);
//Add a line again
Cgcontextaddlinetopoint (CTX, 150, 80);
Set the hollow color
Cgcontextsetrgbstrokecolor (CTX, 1, 0, 0, 1);
Close path
Cgcontextclosepath (CTX);
Rendering
Cgcontextstrokepath (CTX);
3. Quadrilateral
Get context
Cgcontextref CTX = Uigraphicsgetcurrentcontext ();
Add a rectangle
Cgcontextaddrect (CTX, CGRectMake (20, 20, 100, 150));
Set the hollow color
Cgcontextsetrgbstrokecolor (CTX, 0, 0, 1, 1);
Cgcontextstrokepath (CTX);
Note: If you replace the code that sets the hollow color with the solid color
Set Solid color
Cgcontextsetrgbfillcolor (CTX, 1, 0, 0, 1);
Cgcontextfillpath (CTX);
If you replace the set empty code with the set color
Set the color, set equal to both the hollow color and the solid color
[[Uicolor Redcolor] set];
Cgcontextfillpath (CTX);
4. one-eighth Yuan
Get context
Cgcontextref CTX = Uigraphicsgetcurrentcontext ();
Starting point
Cgcontextmovetopoint (CTX, 100, 100);
//Add segment
Cgcontextaddlinetopoint (CTX, 150, 100);
Drawing arcs, Parameter parsing: context, center X, center y, radius, starting angle, end angle, counterclockwise (1) or clockwise (0)
Cgcontextaddarc (CTX, 0, m_pi_4, 0);
Close path
Cgcontextclosepath (CTX);
//Set color
[[Uicolor Redcolor] set];
//Rendering
Cgcontextfillpath (CTX);
5. Arc
Cgcontextref CTX = Uigraphicsgetcurrentcontext ();
Cgcontextaddarc (CTX, M_pi_4, M_PI, 0);
[[Uicolor Redcolor] set];
Cgcontextfillpath (CTX);
6. Ellipse
Cgcontextref CTX = Uigraphicsgetcurrentcontext ();
Cgcontextaddellipseinrect (CTX, CGRectMake (100, 100, 150, 100)); As long as the width = high, the draw is a positive circle
[[Uicolor Redcolor] set];
Cgcontextstrokepath (CTX);
7. Drawing pictures
UIImage *img = [UIImage imagenamed:@ "aa.jpg"];
Drawing with the origin of coordinates (50,50), the length and width are the original length and width of the picture.
[img Drawatpoint:cgpointmake (50, 50)];
Picture is drawn in a rect, and if the aspect ratio picture's original length is wide, the picture will be stretched
[img Drawinrect:cgrectmake (50, 50, 100, 100)];
[img Drawaspatterninrect:cgrectmake (50, 50, 200, 200)]; //Draw in Rect in a tiled way
8. Drawing text
Cgcontextref CTX = Uigraphicsgetcurrentcontext ();
Draw Rect
CGRect cuberect = CGRectMake (50, 50, 100, 100);
Cgcontextaddrect (CTX, cuberect);
Cgcontextfillpath (CTX);
Draw text in Rect
NSString *str = @ "Try quartz2d to draw text ...";
Nsmutabledictionary *attrs = [Nsmutabledictionary dictionary];
Attrs[nsforegroundcolorattributename] = [Uicolor redcolor]; //Text color
Attrs[nsfontattributename] = [Uifont systemfontofsize:50]; //Font
[str drawinrect:cuberect withattributes:attrs];
IOS quartz2d Drawing Basic graphics