- To draw with the CG framework:
- 1. Subclasses of UIView
- 2. Replication DrawRect method in Graphicview
- 3. Get the Graphics context object
- 4. Draw lines
- (1) First create a path, the line is defined in this path, and then the path to the context to display.
- (2) Draw the line directly to the context.
- 5. Draw Simple Graphics
- 6. Drawing text
10.7. Drawing an image
12.-(void) DrawRect: (cgrect) Rect {
13.//1. Getting the graphics context
- Cgcontextref context = Uigraphicsgetcurrentcontext ();//Graphics contexts
- [Self drawimage:context];
17.}
19.////Drawing Line Method one
20.//2. Creating a drawing Path
- Cgmutablepathref path = cgpathcreatemutable ();
22.//add a starting point on the path
- Cgpathmovetopoint (Path, NULL, 50, 50);
- Cgpathaddlinetopoint (Path, NULL, 100, 100);
25.//closes the path when the drawing is complete, which connects the start and end points
- Cgpathclosesubpath (path);
27.//3. Adding a path to a graphics context
- Cgcontextaddpath (context, path);
29.//4. Setting properties for a graphics context
- Set line width
- Cgcontextsetlinewidth (context, 5);
- Set Line Color
- Cgcontextsetrgbstrokecolor (context, 254.0/255, 201.0/255, 21.0/255, 1);
- Fill Color
- Cgcontextsetrgbfillcolor (context, 0, 1, 0, 1);
36.//5. Drawing a path on a graphics context
- /*
- Drawing mode:
- Kcgpathfill: Filled (solid)
- Kcgpathstroke: Draw Line (hollow)
- Kcgpathfillstroke: Draw line and fill
- */
- Cgcontextdrawpath (context, kcgpathfillstroke);
44.//6. Release path, there is a create will have release
- Cgpathrelease (path);
47.////Draw Line Method two
48.CGPoint points[] = {{50,50},{100,100},{50,100},{50,50}};
- Cgcontextaddlines (context, points, 4);
50.//Setting Line Colors
- [[Uicolor Redcolor] setstroke];
52.//Setting the Fill color
- [[Uicolor Bluecolor] setfill];
54.//setting line and fill colors at the same time
- [[Uicolor Redcolor] set];
56.CGContextDrawPath (context, kcgpathfillstroke);
58.//drawing rectangles
59.//1. Using the functions provided by CG
- CGRect rect = CGRectMake (10, 10, 100, 200);
- Add to Graph context
- Cgcontextaddrect (context, rect);
- Setting the properties of a context
- [[Uicolor Redcolor] set];
- Cgcontextdrawpath (context, kcgpathfillstroke);
66.//2. Functions provided using the Uikit framework
- CGRect rect = CGRectMake (10, 10, 100, 200);
- Setting the properties of a context
- [[Uicolor Redcolor] set];
- Draw a filled rectangle
- Uirectfill (rect);
- Draw a hollow rectangle
- Uirectframe (rect);
76.//Draw a circle
77.//1.
- coordinates of x, y Center
- Radius: Radius
- Startangel: Starting angle
- Endangel: End of angle
- clockwise:0 Clockwise, 1 counterclockwise
- Cgcontextaddarc (context, 0, m_pi_4, 0);
84.//2. Given a rectangle, draw a inscribed circle
- CGRect rect = CGRectMake (50, 50, 100, 100);
- [[Uicolor Redcolor] set];
- Uirectframe (rect);
- Cgcontextaddellipseinrect (context, rect);
- Cgcontextdrawpath (context, kcgpathfillstroke);
91.//Drawing Bezier Curves
92.//Setting the starting point
- Cgcontextmovetopoint (context, 20, 200);
- Cgcontextaddcurvetopoint (context, 100, 20, 200, 300, 300, 50);
- Cgcontextaddquadcurvetopoint (context, 150, 20, 300, 200);
- [[Uicolor Redcolor] set];
- Cgcontextdrawpath (context, kcgpathstroke);
98.//drawing a text
- NSString *str = @ "Hello world";
- CGRect rect = CGRectMake (50, 50, 200, 300);
- [[Uicolor Whitecolor] setfill];
- Uirectfill (rect);
- Uifont *font = [Uifont systemfontofsize:20];
- Uicolor *color = [Uicolor Redcolor];
- Create a paragraph style object that sets the style of the text to be displayed in a paragraph by setting its properties.
- Nsmutableparagraphstyle *style = [[Nsmutableparagraphstyle alloc] init];
- Style.linebreakmode = nslinebreakbywordwrapping;
- Style.alignment = Nstextalignmentcenter;
- [Str drawinrect:rect withfont:font linebreakmode:nslinebreakbywordwrapping Alignment:nstextalignmentcenter];
- [Str drawinrect:rect withattributes:
- @{
- Nsfontattributename:font,
- Nsforegroundcolorattributename:color,
- Nsparagraphstyleattributename:style
- }];
- This is a very lightweight way to draw text, and you don't need to create a Uilabel control, and you can tailor the effect to your needs.
- Drawing pictures
- 1. Use the method provided by Uikit to draw
- UIImage *image = [UIImage imagenamed:@ "mp3.jpg"];
- Picture Extrude appears in the specified rectangular area
- [Image drawinrect:cgrectmake (0, 0, 320, 200)];
- The picture tile appears in the specified area
- [Image drawaspatterninrect:cgrectmake (0, 0, 320, 200)];
- 2. Draw with the function provided by coregraphic
- The coordinate system of CG coordinate system transformed into Uikit
- Cgcontextsavegstate (context);
- (1) Clockwise rotation 180 degrees
- CGCONTEXTROTATECTM (context, M_PI);
- (2) x zoom to original-1 time times
- CGCONTEXTSCALECTM (Context,-1, 1);
- (3) Pan up
- CGCONTEXTTRANSLATECTM (context, 0,-image.size.height);
- Cgcontextdrawimage (context, CGRectMake (0, 0, Image.size.width, image.size.height), image. Cgimage);
- (4) Restore context
- Cgcontextrestoregstate (context);
Quartz 2D Simple Drawing