IOS-Core Graphic drawing
* Redefine the UIView and draw it in drawRect :( CGRect) rect;
1. Draw a path
_context=UIGraphicsGetCurrentContext(); CGMutablePathRef path=CGPathCreateMutable(); CGPathMoveToPoint(path, NULL, 50, 50); CGPathAddLineToPoint(path, NULL, 150, 150); CGPathAddLineToPoint(path, NULL, 50, 150); CGPathCloseSubpath(path); CGContextAddPath(_context, path); CGFloat myColor[4]={1.0f,1.0f,0,1.0f}; CGContextSetFillColor(_context,myColor); CGContextSetStrokeColor(_context, myColor); CGContextSetShouldAntialias(_context, NO); CGContextSetLineWidth(_context, 15.0); CGContextSetLineJoin(_context, kCGLineJoinRound); CGContextDrawPath(_context, kCGPathStroke); CGPathRelease(path); CGContextRelease(_context);
2. Draw a rectangle
CGRect rect2=CGRectMake(0, 0, 200, 200); CGContextSetStrokeColorWithColor(_context, [UIColor redColor].CGColor); CGContextSetLineWidth(_context, 2.0); CGContextStrokeRect(_context, rect2);
3. Draw a gradient
_ Context = UIGraphicsGetCurrentContext (); // color mode CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB (); // build the color value NSArray * colors = @ [(_ bridge id) [UIColor redColor]. CGColor, (_ bridge id) [UIColor greenColor]. CGColor]; // The color position CGFloat location [2] = {0, 1}; // you can specify CGGradientRef gradient = CGGradientCreateWithColors (colorSpace, (_ bridge CFArrayRef) colors, location); CGContextSaveGState (_ context); CGRect rect2 = CGRectMake (0, 0,150,150); // draw area CGContextAddRect (_ context, rect2 ); // region crop CGContextClip (_ context); // crop position CGContextDrawLinearGradient (_ context, gradient, CGPointMake (0, 0), CGPointMake (150,150), kCGGradientDrawsAfterEndLocation ); CGContextRestoreGState (_ context); // release the resource cgcolorspacerelshort (colorSpace); CGGradientRelease (gradient); CGContextRelease (_ context );
4. Draw text
NSString *str=[UIFont fontWithName:@"Gill Sans" size:30]; NSString *drawText=@"hello !"; [drawText drawAtPoint:CGPointMake(30, 100) withFont:str];
5. Draw Images
UIImage * image = [UIImage imageNamed: @ "header 2.png"]; [image drawAtPoint: CGPointMake (30, 30)]; CGImageRelease ([image CGImage]);
6. beiser curve drawing
// Draw line UIBezierPath * path = [UIBezierPath bezierPath]; [path moveToPoint: CGPointMake (30, 30)]; [path addLineToPoint: CGPointMake (30,120)]; [path addLineToPoint: CGPointMake (120,120)]; [path closePath]; // draw a circle UIBezierPath * path = [UIBezierPath bezierPathWithArcCenter: CGPointMake (100,100) radius: 100.0 startAngle: 0 endAngle: 180 clockwise: YES]; // draw the rectangle UIBezierPath * path = [UIBezierPath paths: CGRectMake (20, 20,150,150)]; // draw the Oval UIBezierPath * path = [UIBezierPath labels: CGRectMake (10, 10,120, 60)]; // set the color [[UIColor redColor] set]; path. lineWidth = 5; [path fill]; [[UIColor yellowColor] set]; [path stroke];
7. Save the painting environment. During the painting process, attributes may need to be saved to the stack. At this time, CGContextSaveGState is called for saving,
And use CGContextRestoreGState to restore the database as needed.