CoreGraphics也称为Quartz 2D 是UIKit下的主要绘图系统,频繁的用于绘制自定义视图。Core Graphics是高度集成于UIView和其他UIKit部分的。Core Graphics数据结构和函数可以通过前缀CG来识别。
先简单粗暴,上五个常用实例
视图可以通过子视图、图层或实现drawRect:方法来表现内容,如果说实现了drawRect:方法,那么最好就不要混用其他方法了,如图层和子视图。自定义绘图大部分是由UIKit或者Core Graphics来实现的。
实例
1、绘制文本
1  | CGContextRef context4 = UIGraphicsGetCurrentContext();  | 
2、绘制图片
1  | UIImage * img = [UIImage imageNamed:@"bg"];  | 
3、渐变效果
1  | CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();  | 
4、画矩形
1  | CGContextRef context3 = UIGraphicsGetCurrentContext();  | 
5、画线
1  | [[UIColor redColor] setFill];  | 
CGContextRef
Graphics Context是图形上下文,也可以理解为一块画布,我们可以在上面进行绘画操作,绘制完成后,将画布放到我们的view中显示即可,view看作是一个画框。
接下来开始CGContextRef的相关方法介绍,方法较多,大家可筛选重点来看。
相关枚举值
- 线段端点的绘制形状:
 
1  | typedef CF_ENUM(int32_t, CGLineJoin) {  | 
设置方法:
1  | void CGContextSetLineCap(CGContextRef__nullable c, CGLineCap cap)  | 
- 连接点样式:
 
1  | typedef CF_ENUM(int32_t, CGLineJoin) {  | 
[图片上传失败…(image-b758c4-1557146885538)]
设置方法:
1  | void CGContextSetLineJoin(CGContextRef__nullable c, CGLineJoin join)  | 
当把连接点风格设为meter风格时,该方法用于控制锐角箭头的长度:
1  | void CGContextSetMiterLimit(CGContextRef__nullable c, CGFloat limit)  | 
- 绘制模式:
 
1  | typedef CF_ENUM (int32_t, CGPathDrawingMode) {  | 
绘制的时候可以用到
1  | void CGContextDrawPath(CGContextRef__nullable c, CGPathDrawingMode mode)  | 
方法-状态相关:
- 保存绘图状态:
 
1  | //为了让开发者在进行坐标变换时无须计算多次坐标变换后的累加结果,Quartz 2D还提供了如下两个方法来保存、恢复绘图状态  | 
- 把CGContextRef恢复到最近一次保存的状态:
 
1  | void CGContextRestoreGState(CGContextRef__nullable c)  | 
方法-更改坐标系统:
- 缩放坐标系统:
 
1  | //该方法控制坐标系统水平方向上缩放 sx,垂直方向上缩放 sy。在缩放后的坐标系统上绘制图形时,所有点的 X 坐标都相当于乘以 sx 因子,所有点的 Y 坐标都相当于乘以 sy因子。  | 
- 平移坐标系统:
 
1  | // 该方法相当于把原来位于 (0, 0) 位置的坐标原点平移到 (tx, ty)点。在平移后的坐标系统上绘制图形时,所有坐标点的 X坐标都相当于增加了 tx,所有点的 Y坐标都相当于增加了 ty。  | 
- 旋转坐标系统:
 
1  | //该方法控制坐标系统旋转 angle 弧度。在缩放后的坐标系统上绘制图形时,所有坐标点的 X、Y坐标都相当于旋转了 angle弧度之后的坐标。  | 
- 矩阵变换
 
通过矩阵设置变换:
1  | //使用 transform变换矩阵对 CGContextRef的坐标系统执行变换,通过使用坐标矩阵可以对坐标系统执行任意变换。  | 
拿到变换中的矩阵:
1  | CGAffineTransform CGContextGetCTM(CGContextRef__nullable c)  | 
方法-设置:
- 设置绘制直线、边框时的线条宽度:
 
1  | void CGContextSetLineWidth(CGContextRef__nullable c, CGFloat width)  | 
- 设置虚线模式:
 
1  | // Linedash pattern(虚线模式)允许我们沿着描边绘制虚线。我们通过在CGContextSetLineDash结构体中指定虚线数组和虚线相位来控制虚线的大小及位置。  | 
- 设置弯曲的路径中的图形上下文的准确性
 
1  | void CGContextSetFlatness(CGContextRef__nullable c, CGFloat flatness)  | 
- 设置全局透明度
 
1  | void CGContextSetAlpha(CGContextRef__nullable c, CGFloat alpha)  | 
- 设置CGContextRef的叠加模式。Quartz 2D支持多种叠加模式:
 
1  | void CGContextSetBlendMode(CGContextRef __nullable c, CGBlendMode mode)  | 
方法-路径变换:
- 开创新路径:
 
1  | void CGContextBeginPath(CGContextRef__nullable c)  | 
- 开始一个新的子路径起点:
 
1  | void CGContextMoveToPoint(CGContextRef__nullable c,  | 
- 添加一条直线段:
 
1  | void CGContextAddLineToPoint(CGContextRef__nullable c,  | 
- 添加多条直线路径:
 
1  | void CGContextAddLines(CGContextRef__nullable c,  | 
- 添加一个三次bezier曲线:
 
1  | /**  | 
- 添加一个二次bezier曲线:
 
1  | /**  | 
- 关闭子路径,并连接当前点和起点。
 
1  | void CGContextClosePath(CGContextRef__nullable c)  | 
- 添加矩形路径:
 
1  | void CGContextAddRect(CGContextRef__nullable c, CGRect rect)  | 
- 添加多个矩形路径:
 
1  | void CGContextAddRects(CGContextRef__nullable c,  | 
- 添加一个椭圆:
 
1  | void CGContextAddEllipseInRect(CGContextRef__nullable c, CGRect rect)  | 
- 添加一个弧形对象:
 
1  | /**  | 
使用一个序列的三次贝塞尔曲线创建一个弧:
原理:首先画两条线,这两条线分别是 current point to (x1,y1)和(x1,y1) to (x2,y2).这样就是出现一个以(x1,y1)为顶点的两条射线,然后定义半径长度,这个半径是垂直于两条射线的,这样就能决定一个圆了,如果当前点和第一个切点的弧(起点)是不平等的,那么会添加一条直线段从当前指向第一个切点。弧的终点成为新的当前点的路径。
1  | 
  | 
- 添加路径到上下文:
 
1  | void CGContextAddPath(CGContextRef__nullable c, CGPathRef__nullable path)  | 
使用绘制当前路径时覆盖的区域作为当前CGContextRef中的新路径
举例来说,假如当前CGContextRef包含一个圆形路径且线宽为10,调用该方法后,当前CGContextRef将包含一个环宽为10的环形路径
填充该路径包围的区域:
1  | void CGContextFillPath(CGContextRef__nullable c)  | 
- 使用奇偶规则来填充该路径包围的区域:
 
奇偶规则指:如果某个点被路径包围了奇数次,系统绘制该点;如果被路径包围了偶数次,系统不绘制
1  | void CGContextEOFillPath(CGContextRef__nullable c)  | 
- 使用当前 CGContextRef设置的线宽绘制路径
 
1  | void CGContextStrokePath(CGContextRef__nullable c)  | 
- 填充rect代表的矩形
 
1  | void CGContextFillRect(CGContextRef__nullable c, CGRect rect)  | 
- 填充多个矩形:
 
1  | void CGContextFillRects(CGContextRef__nullable c,  | 
- 使用当前 CGContextRef设置的线宽绘制矩形框
 
1  | void CGContextStrokeRect(CGContextRef__nullable c, CGRect rect)  | 
- 使用指定线宽绘制矩形框:
 
1  | void CGContextStrokeRectWithWidth(CGContextRef__nullable c,  | 
- 擦除指定矩形区域上绘制的图形:
 
1  | void CGContextClearRect(CGContextRef__nullable c, CGRect rect)  | 
- 填充rect矩形的内切椭圆区域:
 
1  | void CGContextFillEllipseInRect(CGContextRef__nullable c,CGRect rect)  | 
- 使用当前 CGContextRef设置的线宽绘制rect矩形的内切椭圆:
 
1  | void CGContextStrokeEllipseInRect(CGContextRef__nullable c, CGRect rect)  | 
- 绘制多个线段。
 
该方法需要传入2N个CGPoint组成的数组,其中1、2个点组成第一条线段,3、4个点组成第2条线段,以此类推.
1  | void CGContextStrokeLineSegments(CGContextRef__nullable c,const CGPoint * __nullable points, size_t count)  | 
- 修改当前剪切路径:
 
1  | /* 修改当前剪贴路径,使用非零绕数规则。 */  | 
- 剪切遮罩处理(针对图片)
 
1  | void CGContextClipToMask(CGContextRef__nullable c, CGRect rect, CGImageRef__nullable mask)  | 
- 剪切矩形外的部分.
 
1  | /* 剪切指定矩形区域外的部分. */  | 
- 使用指定颜色来设置该CGContextRef的填充颜色
 
1  | void CGContextSetFillColorWithColor(CGContextRef__nullable c,CGColorRef __nullable color)  | 
- 使用指定颜色来设置该CGContextRef的线条颜色
 
1  | void CGContextSetStrokeColorWithColor(CGContextRef__nullable c,CGColorRef __nullable color)  | 
方法-读取
- 返回一个非空的路径中的当前点:
 
1  | CGPoint CGContextGetPathCurrentPoint(CGContextRef__nullable c)  | 
- 返回当前路径的最小矩形:
 
1  | CGRect CGContextGetPathBoundingBox(CGContextRef__nullable c)  | 
- 返回拷贝后的一个path:
 
1  | CGPathRef __nullableCGContextCopyPath(CGContextRef__nullable c)  | 
- 获取到了需要绘制的图形上下文的位置与大小
 
1  | CGRect CGContextGetClipBoundingBox(CGContextRef__nullable c)  | 
1  | bool CGContextIsPathEmpty(CGContextRef__nullable c)  | 
- 检查当前路径是否包含指定的点:
 
1  | bool CGContextPathContainsPoint(CGContextRef__nullable c,  | 
其它方法
1  | /** Color space functions. **/  |