When we first came into contact with Quertz 2D, it was important that the drawing was performed in the context of the drawing. Each view has an associated context.
Therefore, if you draw a graph in a view, we need to retrieve the current context.
CGContextRef ctx =
UIGraphicsGetCurrentContext ();
// Retrieve the current context. Each view of the drawing has an associated context, and then pass the context to the core graphics drawing function to draw
CGContextSetLineWidth (ctx, 3.0); // you can specify 2 pixels for a straight line;
CGContextSetStrokeColorWithColor (ctx, [UIColor greenColor]. CGColor); // specify that the stroke color is green;
Note that there are two different color settings in the drawing operation:
The stroke color is used to draw a straight line or a Shape Contour (such as a circular square)
Fill color is used to fill the shape (inside)
CGContextMoveToPoint (ctx, 0.0f, 0.0f); // the starting point of a straight line
CGContextAddLineToPoint (ctx,
100,100); // The end of a straight line. When this function is called, The Invisible stroke is moved to a new position, but no content is drawn.
CGContextStrokePath (ctx); // This function draws a straight line.
- Cgcontextsetrgbfillcolor/cgcontextsetfillcolorwithcolor // fill color
- Cgcontextsetrgbstrokecolor/cgcontextsetstrokecolorwithcolor // stroke color
- Cgcontextsetlinewidth // line width
Another important property of drawing is color.
In common development applications, we usually use UIColor to set the color. However, in Quertz 2D, we cannot directly call the UIColor object, but UIColor contains the CGColor attribute.
@ Property (nonatomic,
Readonly) cgcolorref cgcolor; so we can use [uicolor bluecolor]. cgcolor when setting the color.
The above briefly introduces the line and CGColor, but what if we need to use Core Graphics to operate CGImage. At this time, we need to specify a CGPoint to determine the upper left corner of the image or specify a CGRect to frame the image. It is appropriate to adjust the image size period as needed.
CGPoint drawPoint = CGPointMake (100366f, 11.0f );
[Image drawAtPoint: drawPoint];
Draw shape:
Draw an elliptical shape: CGRect rect = CGRectMake (0, 0,100,100 );
CGContextAddEllipseInRect (ctx, rect );
CGContextDrawPath (ctx,
KCGPathFillStroke );
CGContextAddRect (CGContextRef c,
CGRect rect)
CGContextAddRects (CGContextRef c,
Const cgrect rects [], size_t count];
CGContextAddLines (CGContextRef c,
Const CGPoint points [], size_t count];
CGContextAddEllipseInRect (CGContextRef context,
CGRect rect)
Cgcontextaddarc
CGContextAddArcToPoint
Cgcontextaddpath (cgcontextref context,
Cgpathref path)