//画线
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(context, 100.f,100.f);
CGContextAddLineToPoint(context, 200.f, 200.f);
CGContextStrokePath(context);
//画折线
CGContextRef context = UIGraphicsGetCurrentContext();
CGPoint lines[] =
{
CGPointMake(100.f,100.f),
CGPointMake(200.f,30.f),
CGPointMake(40.f,150.f),
CGPointMake(70.f,400.f),
CGPointMake(10.f,100.f),
CGPointMake(100.f,10.f),
CGPointMake(100.f,100.f),
};
CGContextAddLines(context, lines, sizeof(lines)/sizeof(lines[0]));
CGContextStrokePath(context);
//画平行线(两个点一组,画多条线段)
CGContextRef context = UIGraphicsGetCurrentContext();
CGPoint lines[] =
{
CGPointMake(10.f,200.f),
CGPointMake(50.f,100.f),
CGPointMake(90.f,200.f),
CGPointMake(130.f,100.f),
CGPointMake(170.f,200.f),
CGPointMake(210.f,100.f),
};
CGContextAddLines(context, lines, sizeof(lines)/sizeof(lines[0]));
CGContextStrokeLineSegments(context, lines, sizeof(lines)/sizeof(lines[0]));
CGContextStrokePath(context);
//画矩形
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddRect(context, CGRectMake(20, 20, 100, 300));
CGContextStrokePath(context);
//画多个矩形(可以话表格)
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddRect(context, CGRectMake(20, 20, 100, 300));
CGRect rects[] = {
CGRectMake(20,20, 30, 30),
CGRectMake(40,20, 30, 30),
CGRectMake(20,70, 20, 30),
CGRectMake(80,20, 30, 10),
CGRectMake(200,20, 70, 30),
};
CGContextAddRects(context, rects, sizeof(rects)/sizeof(rects[0]));
CGContextStrokePath(context);
//画圆
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddEllipseInRect(context, CGRectMake(30, 30, 60, 30));
CGContextStrokePath(context);
//画实心圆
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextBeginPath(context);
CGRect circle = CGRectInset(self.bounds, 50, 50);
CGContextAddEllipseInRect(context, circle);
CGContextFillPath(context);
//画扇形
CGContextRef context = UIGraphicsGetCurrentContext();
//设置圆心点(不写为半弧形)
CGContextMoveToPoint(context, 130, 130);
//设置属性(扇形)扇形圆心坐标 XY 半径 起点角度 终点角度 画取方向(0=逆时针,1=顺时针)
CGContextAddArc(context, 130,130, 70.f,-270, 180,1);
//闭合
CGContextClosePath(context);
//实心
CGContextFillPath(context);
//空心
CGContextStrokePath(context);
//清除区域
CGContextClearRect(context, CGRectMake(100,100, 80, 50));
//画渐变色
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorSpaceRef color = CGColorSpaceCreateDeviceRGB();
UIColor *startColor = [UIColorblueColor];
CGFloat *startColorComp = (CGFloat *)CGColorGetComponents(startColor.CGColor);
UIColor *endColor = [UIColoryellowColor];
CGFloat *endColorComp = (CGFloat*)CGColorGetComponents(endColor.CGColor);
CGFloat colorComponents[8]= {
startColorComp[0],
startColorComp[1],
startColorComp[2],
startColorComp[3],
endColorComp[0],
endColorComp[1],
endColorComp[2],
endColorComp[3]
};
CGFloat colorIndices[2]= {
0.f,
1.f
};
CGGradientRef gradient = CGGradientCreateWithColorComponents(color,colorComponents, colorIndices, 2);
CGContextDrawLinearGradient(context, gradient, CGPointMake(120,260), CGPointMake(200, 230), 0);
//设置阴影
CGContextSetShadow(context, CGSizeMake(20, 20), 20);
//绘位图
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawImage(context, CGRectMake(100,100, 200,300), [UIImageimageNamed:@"096.jpg"].CGImage);
//绘字
CGContextRef context = UIGraphicsGetCurrentContext();
CGFontRef font = CGFontCreateWithFontName((CFStringRef)@"Helvetica");
CGContextSetFont(context, font);
CGContextSetFontSize(context, 100.f);
CGContextSetTextMatrix(context, CGAffineTransformMakeScale(1.0, -1.0));
CGPoint points[] = {
CGPointMake(150.f,-200.f)
};
CGGlyph glays[1]= {90};
CGContextShowGlyphsAtPositions(context, glays,points,3);
self.layer.shouldRasterize = YES;