1. Quartz2D基本概念
Quartz2D是一个二维图形绘制引擎。
Quartz2D实现了很多的功能,如基于路径的绘图,透明度,渐变,颜色,PDF文档生成
Quartz2D是Core Graphic框架的一部分,因此API中的很多数据类型和方法都是以CG开头。
2. Quartz2D绘图的基本步骤
1.获取与视图相关联的上下文对象CGContextRef
可以调用这个API获取CGContextRef:UIGraphicsGetCurrentContext
2.创建及设置路径
创建路径:CGMutablePathRef path=CGPathCreateMutable();
设置路径起点: CGPathMoveToPoint(path, NULL, 160,50);
增加路径内容: CGPathAddLineToPoint(path, NULL, 40,200);
3.将路径添加到上下文CGContextRef: CGContextAddPath(context, path);
4.设置上下文的一些属性
边线颜色,填充颜色,线宽,线段连接样式,线段收尾连接样式,虚线样式,GContextSet方法
5.绘制路径
CGContextDrawPath(context, kCGPathFillStroke);
6.释放路径
CGPathRelease(path)
3. Quartz2D绘制线段
-(void)drawLine1:(CGContextRef) context{
//创建路径并设置路径,此处是create打头,所以到最后需要释放
CGMutablePathRefpath=CGPathCreateMutable();
//设置起点
CGPathMoveToPoint(path,NULL,160,50);
//设置线段目标点
CGPathAddLineToPoint(path,NULL,280,200);
CGPathAddLineToPoint(path,NULL,40,200);
//封闭路径,自动回到起点
CGPathCloseSubpath(path);
//将路径添加到上下文
CGContextAddPath(context,path);
/*
*设置上下文一些属性
*1.设置线条到颜色
*/
CGContextSetRGBStrokeColor(context,1.0,0.0,0.0,1.0);
//2.设置填充颜色
CGContextSetRGBFillColor(context,0.0,1.0,0.0,1.0);
//3.设置线段宽度
CGContextSetLineWidth(context,5.0);
//4.设置顶点样式
CGContextSetLineCap(context,kCGLineCapRound);
//5.设置连接点样式
CGContextSetLineJoin(context,kCGLineJoinRound);
//6.设置为虚线
/**
phase,相位
lengths:虚线数组
count:数组长度
*/
CGFloat dashArray[]={5,10};
CGContextSetLineDash(context,0.0,dashArray, 2);
//绘制路径(即画线又填充)
CGContextDrawPath(context,kCGPathFillStroke);
//释放路径
CGPathRelease(path);
CGContextRef自带路径,所以代码还可以这样写
//设置起点
CGContextMoveToPoint(context,160,50);
//设置线段目标点
CGContextAddLineToPoint(context,280,200);
CGContextAddLineToPoint(context,40,200);
//封闭路径,自动回到起点
CGContextClosePath(context);
4. Quartz2D绘制矩形
//创建矩形
CGRect rect1=CGRectMake(60, 50, 200, 100);
//设置填充颜色
[[UIColor cyanColor] setFill];
//绘制实心矩形
UIRectFill(rect1);
//设置线条颜色
[[UIColor blueColor] setStroke];
CGRect rect2=CGRectMake(60, 170, 200, 100);
CGFloat dash[2]={10,10};
//设置虚线
CGContextSetLineDash(context,0.0,dash, 2);
//Draw rect don't fill
UIRectFrame(rect2);
5. Quartz2D绘制圆形
UIRectFrame(rect1);
//setting path
CGContextAddEllipseInRect(context, rect1);
//drawpaht, not fill.
CGContextDrawPath(context, kCGPathStroke);
6. Quartz2D绘制弧形
/*参数
x,y圆心
radius半径
starAngle起始角度
endAngle结束角度
clockwise顺时针or逆时针
*/
CGContextAddArc(context,160,200,100,-M_2_PI,0,0);
CGContextMoveToPoint(context,160.0,200.0);
CGContextAddLineToPoint(context,260,200);
floatlens=sinf(M_2_PI)*100.0;
floatlenc=cosf(M_2_PI)*100.0;
CGPoint point=CGPointMake(160+lenc, 200-lens);
CGContextAddLineToPoint(context,point.x, point.y);
CGContextClosePath(context);
CGContextDrawPath(context, kCGPathStroke);
7. Quartz2D绘制文本
NSString *text=@"HelloSteven [text drawAtPoint:CGPointMake(50,80) withFont:[UIFontsystemFontOfSize:16.0]];";
//[text drawAtPoint:CGPointMake(50, 80) withFont:[UIFontsystemFontOfSize:16.0]];
CGRect rect=CGRectMake(50, 80, 220, 300);
UIRectFrame(rect);
//draw text inspectied rect.
[text drawInRect:rect withFont:[UIFont systemFontOfSize:16.0] lineBreakMode:NSLineBreakByWordWrapping];
8. Quartz2D绘制图片
UIImage *image=[UIImage imageNamed:@"demo.jpg"];
//draw imagein specified rect
[image drawInRect:CGRectMake(40, 100, 251, 164)];
9. 下载源代码