Quartz2D drawing and iosquartz2d drawing developed by IOS
// Customize the drawing and call the drawRect Method
-(Void) drawRect :( CGRect) rect {
// Obtain the context first
CGContextRef context = UIGraphicsGetCurrentContext ();
// [Self drawLine: context];
}
# Pragma mark-draw a line
-(Void) drawLine :( CGContextRef) context
{
// 1. Obtain the context
// Context
// 2. Create the drawn path (path)
// Path
// The CGPathRef variable
// CGMutablePathRef variable path
CGMutablePathRef path = CGPathCreateMutable ();
// Set the start point
CGPathMoveToPoint (path, NULL, 50, 50 );
// Connect to the next point
CGPathAddLineToPoint (path, NULL, 200,200 );
CGPathAddLineToPoint (path, NULL, 50,200 );
// Close the path
CGPathCloseSubpath (path );
// 3. Add the path to the context
CGContextAddPath (context, path );
// 4. Set context attributes
// Line color
CGContextSetRGBStrokeColor (context, 41/255. 0, 110/255. 0, 222/255. 0, 1 );
// Fill color
CGContextSetRGBFillColor (context, 150/255. 0, 50/255. 0, 100/255. 0, 1 );
// Line width
CGContextSetLineWidth (context, 5 );
// Connection point style
CGContextSetLineJoin (context, kCGLineJoinRound );
// 5. Draw the path
/*
KCGPathFill, // only fill
KCGPathEOFill, // unique or
KCGPathStroke, // draw only lines
KCGPathFillStroke, // fill and draw line
KCGPathEOFillStroke
*/
CGContextDrawPath (context, kCGPathFillStroke );
// 6. Release path
CGPathRelease (path );
}
# Pragma mark-draw a rectangle
-(Void) drawShapeRect :( CGContextRef) context
{
// Method 1
/*
// Set the area to be drawn
CGRect rect = CGRectMake (40, 40,100,200 );
// Add the region to the context
CGContextAddRect (context, rect );
// How to set the color of the UIKit package
[[UIColor redColor] setStroke];
[[UIColor blueColor] setFill];
// Draw
CGContextDrawPath (context, kCGPathFillStroke );
*/
// Method 2
// UIKit
CGRect rect = CGRectMake (40, 40,100,200 );
[[UIColor redColor] setStroke];
[[UIColor blueColor] setFill];
// Call to directly start plotting
UIRectFill (rect); // fill
UIRectFrame (rect); // framework
}
# Pragma mark-draw a circle
-(Void) drawCircle :( CGContextRef) context
{
// Circle
/*
* X, y center
* Radius
* StartAngle endAngle angle (unit: radian)
* Clockwise 0/counter-clockwise 1
*/
// CGContextAddArc (context, 160,200, 50, 0, M_PI_2, 1 );
// Incircle
CGRect rect = CGRectMake (50, 50,200,100 );
[[UIColor orangeColor] setStroke];
UIRectFrame (rect );
CGContextAddEllipseInRect (context, CGRectMake (50, 50,200,100 ));
[[UIColor redColor] setStroke];
[[UIColor brownColor] setFill];
CGContextDrawPath (context, kCGPathFillStroke );
}
# Pragma mark-besell Curve
-(Void) drawCurve :( CGContextRef) context
{
// Start point
CGContextMoveToPoint (context, 20, 50 );
/*
* 1x, 1y end point of the first tangent
* 2x, 2y start point of the second tangent
* The end point of the second tangent of x and y
*/
// CGContextAddCurveToPoint (context, 100, 20,200,300,300, 50 );
CGContextAddQuadCurveToPoint (context, 160, 20,300,200 );
CGContextDrawPath (context, kCGPathStroke );
}
# Pragma mark-draw text
-(Void) drawText :( CGContextRef) context
{
// 1. Drawn text
NSString * str = @ "wxhl34 ";
// 2. Define the drawn Area
CGRect rect = CGRectMake (50, 50,200, 50 );
UIRectFrame (rect );
// 3. Set the drawn style
// Text paragraph style
NSMutableParagraphStyle * style = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
Style. lineBreakMode = NSLineBreakByCharWrapping;
Style. alignment = NSTextAlignmentRight;
NSDictionary * attr = @{
NSFontAttributeName: [UIFont systemFontOfSize: 20],
NSParagraphStyleAttributeName: style
};
// 4. Draw
[Str drawInRect: rect withAttributes: attr];
}
# Pragma mark-draw an image
-(Void) drawImage :( CGContextRef) context
{
// Image
UIImage * image = [UIImage imageNamed: @ "baymax.jpg"];
// 1. Draw from the specified point
[Image drawAtPoint: CGPointMake (20,400)];
// 2. drawing in the specified area will automatically stretch
// [Image drawInRect: CGRectMake (0, 0,100,200)];
// Coordinate System Conversion
// CG UIKit
// Save the current context
CGContextSaveGState (context );
// CGContextTranslateCTM (context, 0,200 );
// Returns the inverse of the Y axis.
CGContextScaleCTM (context, 1,-1 );
// Move the Y axis up 200
CGContextTranslateCTM (context, 0,-200 );
/*
CGContextRotateCTM (context, M_PI); // rotate
CGContextScaleCTM (context,-1, 1); // zoom
CGContextTranslateCTM (context, 0,-200); // Translation
*/
// CG
// UIImage --> CGImageRef
// UIKit Core Graphics AppKit
CGContextDrawImage (context, CGRectMake (0, 0,320,200), image. CGImage );
// Restore the saved Context
CGContextRestoreGState (context );
}