/*
* Drawing: Quarz 2d--"Core Graphics
Role:
Drawing: lines \ triangles \ rectangles \ Circles \ Arcs, etc.
Draw text draw \ Generate picture (image)
Read \ Generate PDF
\ crop a picture
Customizing UI Controls
Graffiti \ Artboard
Gestures to unlock ...
Attention:
The quartz2d API is a pure C language.
The Quartz2d API comes from the core graphics framework that needs to be imported
Coregraphics.framework data types and functions are basically based on CG as prefix cgcontextrefcgpathrefcgcontextstrokepath (CTX);
Steps:
(1) Cgcontextref Context--"artboards
(2) Drawing Content-"set the content of the drawing"
(3) Adding content to the context "artboards"
(4) Draw the content onto the artboard
Introduction to Common methods
(1) Cgcontextref context
(2) path
"1" Uibezierpath
"2" cgmutablepathref a path through a point
"3" Note you must set the starting point Cgcontentextmovetopoint
(3) Drawing graphics
Rectangular Cgcontextaddrect
Curve Cgcontextaddcurvetopoint
Round Cgcontextaddellipseinrect
(3.1) Cgcontextsetlinewidth set the stroke width
(3.2) Set sets the color of the stroke
(3.3) Setfill the fill of the range of the underlined area
(3.4) Setstroke sets the color of the stroke
(3.5) Set brush fill style
"1" Kcgpathfill only fill
"2" Kcgpathstroke brush color
"3" Kcgpathfillstroke both fill and brush color
(4)
(1) uigraphicsbeginimagecontextwithoptions start
(2) Get a picture of the current picture context Uigraphicsgetimagefromcurrentimagecontext ()
(3) Uigraphicsendimagecontext Close picture context
(4) uigraphicsbeginimagecontext start to get picture context
(5) Cgcontextstrokepath to draw the path to the picture context
(6) Draw the path to the interface directly above the stroke
*/
All the drawing code is written in DrawRect.
1. When initialized, it is called
2. Setneedsdisplay
-(void) DrawRect: (CGRect) rect{
[Super Drawrect:rect];
NSLog (@ "DSADSADSA");
[Self addline];
}
Draw a line
-(void) addline{
1. Create the canvas context
Get the current view as context
Cgcontextref context = Uigraphicsgetcurrentcontext ();
/*
Point Center points
*/
2. Create the content of the drawing
* Must have a starting point
* Also have a moving position (another location)
Uibezierpath *path = [Uibezierpath Bezierpath];
2.1 Setting other points
[Path Addlinetopoint:cgpointmake (50, 50)];
[Path Addlinetopoint:cgpointmake (100, 100)];
[Path Addlinetopoint:cgpointmake (300, 300)];
2.2 Setting the width of the brush
Path.linewidth = 2;
2.3 Setting the color of the brush
[[Uicolor Redcolor]set];
Set Brush color
[[Uicolor Whitecolor] setstroke];
Set Fill Color
[[Uicolor Browncolor] setfill];
3. Add the path to the context in the picture
Cgcontextaddpath (context, path. Cgpath);
4. Add the contents of the picture to the artboard
Cgcontextstrokepath (context);
Set the style of the fill
Cgcontextdrawpath (context, kcgpathfillstroke);
}
Draw a line
-(void) addline{
1. Create a canvas context
Get the current context as a canvas
Cgcontextref context = Uigraphicsgetcurrentcontext ();
2. Create Paint Content
Uibezierpath *path = [Uibezierpath Bezierpath];
/*
Point Center points
X Center point X
Y Center Point Y
Y invariant x small value-Large horizontal line
*/
2.1 Setting the starting point
[Path Movetopoint:cgpointmake (10, 50)];
2.2 Adding additional points
[Path Addlinetopoint:cgpointmake (10, 500)];
[Path Addlinetopoint:cgpointmake (300, 500)];
2.3 Setting the brush width
Path.linewidth = 2;
2.4 Setting Brush Colors
[[Uicolor Whitecolor] set];
Set Brush color
[[Uicolor Whitecolor] setstroke];
Set Fill Color
[[Uicolor Browncolor] setfill];
3, the content of the painting "path" added to the context of the canvas
Cgcontextaddpath (context, path. Cgpath);
4. Draw the rendered content to the context "canvas"
Cgcontextstrokepath (context);
Set the style of the fill
Cgcontextdrawpath (context, kcgpathfillstroke);
}
Add a rectangle
-(void) addrect{
1. Canvas
Cgcontextref context = Uigraphicsgetcurrentcontext ();
2. Content
Cgcontextaddrect (context, CGRectMake (0, 0, 100, 100));
[[Uicolor Redcolor]set];
Set the width of a brush
Cgcontextsetlinewidth (context, 3);
3. Rendering
Render rectangles directly
Cgcontextstrokerect (context, CGRectMake (0, 0, 100, 100));
Cgcontextdrawpath (context, kcgpathfillstroke);
}
Draw a Circle
-(void) addround{
Canvas
Content8 = Uigraphicsgetcurrentcontext ();
Content
Cgcontextaddellipseinrect (Content8, CGRectMake (10, 10, 100, 100));
[[Uicolor Blackcolor]set];
Render to Canvas
Cgcontextdrawpath (Content8, Kcgpathfillstroke);
}
Draw a curve
-(void) addcurve{
Canvas
Cgcontextref contenxt = Uigraphicsgetcurrentcontext ();
Content
Uibezierpath *path = [Uibezierpath Bezierpath];
[Path Movetopoint:cgpointmake (100, 100)];
[Path Addcurvetopoint:cgpointmake (Controlpoint1:cgpointmake) (0, 0) controlpoint2:cgpointmake (200, 150)];
/**
* Draw a circle
*
* Cgpointmake Center Point
* Radius radius
* StartAngle the beginning of the arc
* Endangle End of Arc
* is the clockwise clockwise or counterclockwise?
*/
[Path Addarcwithcenter:cgpointmake (radius:100) startangle:m_pi_2 endangle:m_pi Clockwise:yes];
[[Uicolor Redcolor]setstroke];
[[Uicolor Blackcolor]setfill];
Add content to the canvas
Cgcontextaddpath (CONTENXT, path. Cgpath);
Rendering
Cgcontextdrawpath (CONTENXT, Kcgpathfillstroke);
}
Draw Line Simplification
-(void) addline2{
1. Path
2. Draw the Content
Uibezierpath *path = [Uibezierpath Bezierpath];
[Path Movetopoint:cgpointmake (100, 100)];
[Path Addlinetopoint:cgpointmake (200, 500)];
[[Uicolor Redcolor]set];
[Path stroke];
}
Screen Cutting
-(void) cutscreen{
1. Get the context of a picture
2. Context of the canvas
3.
4.
5. Close the context of the picture
6. Save
Uigraphicsbeginimagecontext (self.frame.size);
[Self addround];
[Self.layer Renderincontext:content8];
/*
First parameter picture size
Whether the second parameter is opaque yes opaque no transparent
The third parameter scale
*/
Uigraphicsbeginimagecontextwithoptions (Self.frame.size, YES, 1);
Begin
UIImage *image = Uigraphicsgetimagefromcurrentimagecontext ();
Close context
Uigraphicsendimagecontext ();
Uiimagewritetosavedphotosalbum (image, Self, @selector (image:didFinishSavingWithError:contextInfo:), nil);
}
-(void) Image: (UIImage *) image didfinishsavingwitherror: (nserror *) error ContextInfo: (void *) contextinfo{
}
Use of quartz2d drawing for iOS development