Quartz2D
Quartz2D
是一个二维绘图引擎,同时支持IOS
和Mac OS X
系统(跨平台,纯C语言),包括在CoreGraphics
框架中
- 绘制图形、文字,生成图片
- 读取/生成
PDF
- 截图/裁剪图片
- 自定义
UI
控件
数据类型以CG
作为前缀
Quartz2D的类型
- 图形上下文
CGContextRef
Quartz2D
提供了以下几种类型的Graphics Context
:Bitmap Graphics Context
PDF Graphics Context
Window Graphics Context
Layer Graphics Context
(UI控件
)Printer Graphics Context
(打印机)- 主要包括这些信息:绘图路径(各种各样的图形)、绘图状态(颜色、线宽、样式、旋转、缩放、平移、图片裁剪区域等)、输出目标(输出到哪里)
UIKit框架
- 对部分
Quartz2D
的API
做封装 - 没有封装的只能调用原生的
- 比如:画图片、文字到控件上(
UIKit
已经封装好了)
绘图的步骤
- 绘制到
UIView
上面,新建一个继承自UIView
的类
在其drawRect方法中绘制,方法1 C
- (void)drawRect:(CGRect)rect {
// Drawing code
// 1.获取当前绘图上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 2.把路径添加到上下文当中
CGContextMoveToPoint(ctx,50,50); // 起点 移动笔
CGContextAddLineToPoint(ctx,100,100); // 终点 从起点画到终点
// 2.5可以设置线的样式 可以省略
CGContextSetLineWidth(ctx,10); // 线宽10
CGContextSetLineJoin(ctx,kCGLineJoinRound); // 连接处圆弧,kCGLineJoinMiter默认,kCGLineJoinRound圆弧,kCGLineJoinBevel切角
CGContextSetLineCap(ctx,kCGLineCapRound); // 头尾样式 kCGLineCapButt默认,kCGLineCapRound圆弧,kCGLineCapSquare加个平角(相比默认会长一点)
CGContextSetRGBStrokeColor(ctx,0.4,0.4,0.4,1); // 设置颜色 ctx + RGB三个数值 + 透明度
// 3.渲染
CGContextStrokePath(ctx);
}
方法2 C
- (void)drawRect:(CGRect)rect {
// Drawing code
// 1.获取当前绘图上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 2.拼接路径
CGMutablePathRef path = CGPathCreateMutable();
CGContextMoveToPoint(path,NULL,50,50);
CGContextAddLineToPoint(path,NULL,100,100);
// 3.把路径添加到上下文
CGContextStrokePath(ctx,path);
// 4.渲染
CGContextStrokePath(ctx);
}
方法3 C+OC
- (void)drawRect:(CGRect)rect {
// Drawing code
// 1.获取当前绘图上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 2.拼接路径
// OC封装
UIBezierPath path = [[UIBezierPath alloc] init];
[path moveToPoint