Core Graphics - 基础介绍

CoreGraphics也称为Quartz 2D 是UIKit下的主要绘图系统,频繁的用于绘制自定义视图。Core Graphics是高度集成于UIView和其他UIKit部分的。Core Graphics数据结构和函数可以通过前缀CG来识别。

先简单粗暴,上五个常用实例

视图可以通过子视图、图层或实现drawRect:方法来表现内容,如果说实现了drawRect:方法,那么最好就不要混用其他方法了,如图层和子视图。自定义绘图大部分是由UIKit或者Core Graphics来实现的。

实例

1、绘制文本
1
2
3
4
5
CGContextRef context4 = UIGraphicsGetCurrentContext();
NSString *string = @"我是文本";
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:35], NSFontAttributeName, [UIColor orangeColor], NSForegroundColorAttributeName, nil];
[string drawAtPoint:CGPointMake(30, 450) withAttributes:attrs];
CGContextSaveGState(context4);
2、绘制图片
1
2
UIImage * img = [UIImage imageNamed:@"bg"];
[img drawInRect:CGRectMake(20, 500, 50, 200)];
3、渐变效果
1
2
3
4
5
6
7
8
9
10
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
NSArray *colors = @[(__bridge id)[UIColor colorWithRed:0.3 green:0.0 blue:0.0 alpha:0.2].CGColor,
(__bridge id)[UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:0.8].CGColor];
const CGFloat locations[] = {0.0, 1.0};

CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)colors, locations);
CGContextDrawLinearGradient(context, gradient, CGPointMake(200, 150), CGPointMake(250, 200), 0); //开始绘制
//释放资源
CGGradientRelease(gradient);
CGColorSpaceRelease(colorSpace);
4、画矩形
1
2
3
4
5
6
CGContextRef context3 = UIGraphicsGetCurrentContext();
CGContextAddRect(context, CGRectMake(200, 150, 50, 50));
[[UIColor orangeColor]setStroke];
CGContextSetLineWidth(context3, 3);
CGContextDrawPath(context3, kCGPathStroke);
CGContextSaveGState(context3);
5、画线
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[[UIColor redColor] setFill];
[[UIColor blackColor] setStroke];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 5.0f);
CGContextSetLineJoin(context, kCGLineJoinRound);//设置连接点样式。
CGContextMoveToPoint(context, 50, 100);
CGContextAddLineToPoint(context, 70, 300);
CGContextAddLineToPoint(context, 80, 200);
CGContextClosePath(context);
CGContextDrawPath(context, kCGPathFillStroke);
CGContextSaveGState(context);//保存上下文状态


CGContextRef context2 = UIGraphicsGetCurrentContext();
[[UIColor brownColor]set];
CGContextSetLineWidth(context2, 3);
CGContextMoveToPoint(context2, 200, 100);
CGContextAddLineToPoint(context2, 300, 100);
CGContextAddLineToPoint(context2, 350, 250);
CGContextStrokePath(context2);
CGContextSaveGState(context2);

CGContextRef

Graphics Context是图形上下文,也可以理解为一块画布,我们可以在上面进行绘画操作,绘制完成后,将画布放到我们的view中显示即可,view看作是一个画框。
接下来开始CGContextRef的相关方法介绍,方法较多,大家可筛选重点来看。

相关枚举值
  • 线段端点的绘制形状:
1
2
3
4
5
typedef CF_ENUM(int32_t, CGLineJoin) {
kCGLineJoinMiter, // 这是默认的属性值。该方格的连接点形状如图1所示。
kCGLineJoinRound, // 稍微圆角, 该方格的连接点形状如图2所示。
kCGLineJoinBevel // 斜角,该方格的连接点形状如图3所示。
};

设置方法:

1
void CGContextSetLineCap(CGContextRef__nullable c, CGLineCap cap)
  • 连接点样式:
1
2
3
4
5
typedef CF_ENUM(int32_t, CGLineJoin) {
kCGLineJoinMiter, // 这是默认的属性值。该方格的连接点形状如图1所示。
kCGLineJoinRound, // 稍微圆角, 该方格的连接点形状如图2所示。
kCGLineJoinBevel // 斜角,该方格的连接点形状如图3所示。
};

[图片上传失败…(image-b758c4-1557146885538)]

设置方法:

1
void CGContextSetLineJoin(CGContextRef__nullable c, CGLineJoin join)

当把连接点风格设为meter风格时,该方法用于控制锐角箭头的长度:

1
void CGContextSetMiterLimit(CGContextRef__nullable c, CGFloat limit)
  • 绘制模式:
1
2
3
4
5
6
7
typedef CF_ENUM (int32_t, CGPathDrawingMode) {
kCGPathFill,//只有填充(非零缠绕数填充),不绘制边框 如图1
kCGPathEOFill,//奇偶规则填充(多条路径交叉时,奇数交叉填充,偶交叉不填充)如图2
kCGPathStroke, // 只有边框 如图3
kCGPathFillStroke, // 既有边框又有填充 如图4
kCGPathEOFillStroke // 奇偶填充并绘制边框 如图5
};

绘制的时候可以用到

1
void CGContextDrawPath(CGContextRef__nullable c, CGPathDrawingMode mode)
方法-状态相关:
  • 保存绘图状态:
1
2
3
4
//为了让开发者在进行坐标变换时无须计算多次坐标变换后的累加结果,Quartz 2D还提供了如下两个方法来保存、恢复绘图状态
保存CGContextRef当前的绘图状态,方便以后恢复该状态
// 需要说明的是,CGContextSaveGState()函数保存的绘图状态,不仅包括当前坐标系统的状态,也包括当前设置的填充风格、线条风格、阴影风格等各种绘图状态。但 CGContextSaveGState()函数不会保存当前绘制的图形
void CGContextSaveGState(CGContextRef__nullable c)
  • 把CGContextRef恢复到最近一次保存的状态:
1
void CGContextRestoreGState(CGContextRef__nullable c)
方法-更改坐标系统:
  • 缩放坐标系统:
1
2
//该方法控制坐标系统水平方向上缩放 sx,垂直方向上缩放 sy。在缩放后的坐标系统上绘制图形时,所有点的 X 坐标都相当于乘以 sx 因子,所有点的 Y 坐标都相当于乘以 sy因子。
void CGContextScaleCTM(CGContextRef__nullable c, CGFloat sx, CGFloat sy)
  • 平移坐标系统:
1
2
3
// 该方法相当于把原来位于 (0, 0) 位置的坐标原点平移到 (tx, ty)点。在平移后的坐标系统上绘制图形时,所有坐标点的 X坐标都相当于增加了 tx,所有点的 Y坐标都相当于增加了 ty。
void CGContextTranslateCTM(CGContextRef__nullable c,
CGFloat tx, CGFloat ty)
  • 旋转坐标系统:
1
2
 //该方法控制坐标系统旋转 angle 弧度。在缩放后的坐标系统上绘制图形时,所有坐标点的 X、Y坐标都相当于旋转了 angle弧度之后的坐标。
void CGContextRotateCTM(CGContextRef__nullable c, CGFloat angle)
  • 矩阵变换

通过矩阵设置变换:

1
2
3
 //使用 transform变换矩阵对 CGContextRef的坐标系统执行变换,通过使用坐标矩阵可以对坐标系统执行任意变换。
void CGContextConcatCTM(CGContextRef__nullable c,
CGAffineTransform transform)

拿到变换中的矩阵:

1
CGAffineTransform CGContextGetCTM(CGContextRef__nullable c)
方法-设置:
  • 设置绘制直线、边框时的线条宽度:
1
void CGContextSetLineWidth(CGContextRef__nullable c, CGFloat width)
  • 设置虚线模式:
1
2
3
 // Linedash pattern(虚线模式)允许我们沿着描边绘制虚线。我们通过在CGContextSetLineDash结构体中指定虚线数组和虚线相位来控制虚线的大小及位置。
其中lengths属性指定了虚线段的长度,该值是在绘制片断与未绘制片断之间交替。phase属性指定虚线模式的起始点。图3-11显示了虚线模式:
void CGContextSetLineDash(CGContextRef__nullable c, CGFloat phase,const CGFloat *__nullable lengths, size_t count)
  • 设置弯曲的路径中的图形上下文的准确性
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
2
void CGContextMoveToPoint(CGContextRef__nullable c,
CGFloat x, CGFloat y)
  • 添加一条直线段:
1
2
void CGContextAddLineToPoint(CGContextRef__nullable c,
CGFloat x, CGFloat y)
  • 添加多条直线路径:
1
2
void CGContextAddLines(CGContextRef__nullable c,
const CGPoint * __nullable points, size_t count)
  • 添加一个三次bezier曲线:
1
2
3
4
5
6
7
8
9
10
/**
* 从当前添加一个三次Bezier曲线
* @param cp1x 控制点1 x坐标
* @param cp1y 控制点1 y坐标
* @param cp2x 控制点2 x坐标
* @param cp2y 控制点2 y坐标
* @param x 直线的终点 x坐标
* @param y 直线的终点 y坐标
*/
void CGContextAddCurveToPoint(CGContextRef__nullable c, CGFloat cp1x, CGFloat cp1y, CGFloat cp2x, CGFloat cp2y, CGFloat x, CGFloat y)
  • 添加一个二次bezier曲线:
1
2
3
4
5
6
7
8
/**
* 从当前添加一个二次Bezier曲线
* @param cpx 控制点 x坐标
* @param cpy 控制点 y坐标
* @param x 直线的终点 x坐标
* @param y 直线的终点 y坐标
*/
void CGContextAddQuadCurveToPoint(CGContextRef__nullable c, CGFloat cpx, CGFloat cpy,CGFloat x,CGFloat y)
  • 关闭子路径,并连接当前点和起点。
1
void CGContextClosePath(CGContextRef__nullable c)
  • 添加矩形路径:
1
void CGContextAddRect(CGContextRef__nullable c, CGRect rect)
  • 添加多个矩形路径:
1
2
void CGContextAddRects(CGContextRef__nullable c,
const CGRect * __nullable rects, size_t count)
  • 添加一个椭圆:
1
void CGContextAddEllipseInRect(CGContextRef__nullable c, CGRect rect)
  • 添加一个弧形对象:
1
2
3
4
5
6
7
8
9
10
/**
* 添加弧形对象
* @param x 中心点x坐标
* @param y 中心点y坐标
* @param radius 半径
* @param startAngle 起始弧度
* @param endAngle 终止弧度
* @param clockwise 是否逆时针绘制,0则顺时针绘制
*/
void CGContextAddArc(CGContextRef__nullable c, CGFloat x,CGFloat y,CGFloat radius,CGFloat startAngle,CGFloat endAngle,int clockwise)
  • 使用一个序列的三次贝塞尔曲线创建一个弧:

    原理:首先画两条线,这两条线分别是 current point to (x1,y1)和(x1,y1) to (x2,y2).这样就是出现一个以(x1,y1)为顶点的两条射线,然后定义半径长度,这个半径是垂直于两条射线的,这样就能决定一个圆了,如果当前点和第一个切点的弧(起点)是不平等的,那么会添加一条直线段从当前指向第一个切点。弧的终点成为新的当前点的路径。

1
2

void CGContextAddArcToPoint(CGContextRef__nullable c, CGFloat x1, CGFloat y1, CGFloat x2, CGFloat y2, CGFloat radius)
  • 添加路径到上下文:
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
2
void CGContextFillRects(CGContextRef__nullable c,
const CGRect * __nullable rects, size_t count)
  • 使用当前 CGContextRef设置的线宽绘制矩形框
1
void CGContextStrokeRect(CGContextRef__nullable c, CGRect rect)
  • 使用指定线宽绘制矩形框:
1
2
void CGContextStrokeRectWithWidth(CGContextRef__nullable c,
CGRect rect, CGFloat width)
  • 擦除指定矩形区域上绘制的图形:
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
2
3
4
5
/* 修改当前剪贴路径,使用非零绕数规则。 */
void CGContextClip(CGContextRef__nullable c)

/* 修改当前剪贴路径,使用奇偶规则。 */
void CGContextEOClip(CGContextRef__nullable c)
  • 剪切遮罩处理(针对图片)
1
void CGContextClipToMask(CGContextRef__nullable c, CGRect rect, CGImageRef__nullable mask)
  • 剪切矩形外的部分.
1
2
3
4
5
/* 剪切指定矩形区域外的部分. */
void CGContextClipToRect(CGContextRef__nullable c, CGRect rect)

/* 剪切指定多个矩形区域外的部分 */
void CGContextClipToRects(CGContextRef__nullable c,const CGRect * rects, size_t count)
  • 使用指定颜色来设置该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
2
bool CGContextPathContainsPoint(CGContextRef__nullable c,
CGPoint point, CGPathDrawingMode mode)
其它方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/** Color space functions. **/

/* 颜色空间填充 */
void CGContextSetFillColorSpace(CGContextRef__nullable c, CGColorSpaceRef__nullable space)

/* 设置线框颜色空间 */

void CGContextSetStrokeColorSpace(CGContextRef__nullable c,
CGColorSpaceRef __nullable space)

/** Color functions. **/

/* 设置填充颜色空间 CGFloat redColor[4] = {1.0,0,0,1.0};*/
void CGContextSetFillColor(CGContextRef__nullable c,const CGFloat *__nullable components(redColor))

/* 设置画笔颜色 CGFloat redColor[4] = {1.0,0,0,1.0};*/
void CGContextSetStrokeColor(CGContextRef__nullable c,const CGFloat *__nullable components(redColor))

/** Pattern functions. **/

/*
设置该CGContextRef使用位图填充*/
void CGContextSetFillPattern(CGContextRef__nullable c, CGPatternRef__nullable pattern,const CGFloat * __nullable components)

/*
设置该CGContextRef使用位图绘制线条、边框*/
void CGContextSetStrokePattern(CGContextRef__nullable c, CGPatternRef__nullable pattern,const CGFloat * __nullable components)

/*
设置该CGContextRef采用位图填充的相位*/
void CGContextSetPatternPhase(CGContextRef__nullable c, CGSize phase)

/** Color convenience functions. **/

/*
使用灰色来设置该CGContextRef的填充颜色*/
void CGContextSetGrayFillColor(CGContextRef__nullable c,
CGFloat gray, CGFloat alpha)

/*
使用灰色来设置该CGContextRef的线条颜色*/
void CGContextSetGrayStrokeColor(CGContextRef__nullable c,
CGFloat gray, CGFloat alpha)

/*
使用RGB颜色模式来设置该CGContextRef的填充颜色*/
void CGContextSetRGBFillColor(CGContextRef__nullable c, CGFloat red,
CGFloat green, CGFloat blue, CGFloat alpha)

/* 设置画笔颜色 */
void CGContextSetRGBStrokeColor(CGContextRef__nullable c,
CGFloat red, CGFloat green, CGFloat blue, CGFloat alpha)

/*
使用CMYK颜色模式来设置该CGContextRef的填充颜色*/
void CGContextSetCMYKFillColor(CGContextRef__nullable c,
CGFloat cyan, CGFloat magenta, CGFloat yellow, CGFloat black, CGFloat alpha)

/*
使用CMYK颜色模式来设置该CGContextRef的线条颜色*/
void CGContextSetCMYKStrokeColor(CGContextRef__nullable c,
CGFloat cyan, CGFloat magenta, CGFloat yellow, CGFloat black, CGFloat alpha)

/** Rendering intent. **/

/* 在当前图形状态设置渲染意向 */
void CGContextSetRenderingIntent(CGContextRef__nullable c,
CGColorRenderingIntent intent)

/** Image functions. **/

/* 绘制图像到图形上下文中 */
void CGContextDrawImage(CGContextRef__nullable c, CGRect rect,
CGImageRef __nullable image)

/* 重复绘制的图像,扩展到提供的矩形,填补当前剪辑区域。 */
void CGContextDrawTiledImage(CGContextRef__nullable c, CGRect rect,
CGImageRef __nullable image)

/*
获取当前CGContextRef在放大图片时的插值质量*/
CGInterpolationQuality CGContextGetInterpolationQuality(CGContextRef__nullable c)

/*
设置图形上下文的插值质量水平。*/
void CGContextSetInterpolationQuality(CGContextRef__nullable c,
CGInterpolationQuality quality)

/** Shadow support. **/

/*
设置阴影在X、Y方向上的偏移,以及模糊度和阴影的颜色
*/
void CGContextSetShadowWithColor(CGContextRef__nullable c,
CGSize offset, CGFloat blur, CGColorRef __nullable color)


/*
设置阴影在X、Y方向上的偏移,以及模糊度(blur值越大,阴影越模糊)。该函数没有设置阴影颜色,
默认使用1/3透明的黑色(即RGBA{0, 0, 0, 1.0/3.0})作为阴影颜色
*/

void CGContextSetShadow(CGContextRef__nullable c, CGSize offset,
CGFloat blur)


/** Gradient and shading functions. **/

/* 绘制一个渐变填充定义的出发点和落脚点沿线变化。*/
void CGContextDrawLinearGradient(CGContextRef__nullable c,
CGGradientRef __nullable gradient, CGPoint startPoint, CGPoint endPoint,
CGGradientDrawingOptions options)


/* 绘制一个沿着由所提供的开始和结束的圆限定的区域变化的渐变填充。 */
void CGContextDrawRadialGradient(CGContextRef__nullable c,
CGGradientRef __nullable gradient, CGPoint startCenter, CGFloat startRadius,
CGPoint endCenter, CGFloat endRadius, CGGradientDrawingOptions options)


/* 使用指定的阴影的背景,填充剪切路径。 */
void CGContextDrawShading(CGContextRef __nullable c,
__nullable CGShadingRef shading)


/** Text functions. **/

/* 设置当前字符间距. */
void CGContextSetCharacterSpacing(CGContextRef__nullable c,
CGFloat spacing)


/* 设置要绘制文本的位置。 */
void CGContextSetTextPosition(CGContextRef__nullable c,
CGFloat x, CGFloat y)


/* 返回在绘制文本的位置。 */
CGPoint CGContextGetTextPosition(CGContextRef __nullable c)


/* 设置当前文本矩阵。 */
void CGContextSetTextMatrix(CGContextRef__nullable c,
CGAffineTransform t)

/* 返回当前文本矩阵。 */
CGAffineTransform CGContextGetTextMatrix(CGContextRef__nullable c)

/* 设置当前文本的绘图模式。 */
void CGContextSetTextDrawingMode(CGContextRef__nullable c,
CGTextDrawingMode mode)

/* 设置上下文的字体 */
void CGContextSetFont(CGContextRef__nullable c,
CGFontRef __nullable font)

/* 设置上下文的字体大小。 */
void CGContextSetFontSize(CGContextRef__nullable c, CGFloat size)


/* 在所提供的位置绘制字形。 */
void CGContextShowGlyphsAtPositions(CGContextRef__nullable c,
const CGGlyph * __nullable glyphs, const CGPoint * __nullable Lpositions,
size_t count)


/** PDF functions. **/

/* 绘制一个PDF页面到当前的用户空间。 */
void CGContextDrawPDFPage(CGContextRef__nullable c,
CGPDFPageRef __nullable page)


/** Output page functions. **/

/* 基于页面的图形上下文中开始了新的一页。 */
void CGContextBeginPage(CGContextRef__nullable c,const CGRect *__nullable mediaBox)


/* 在基于页面的图形上下文结束当前的页面。 */
void CGContextEndPage(CGContextRef__nullable c)


/** Context functions. **/

/* 图形上下文的引用计数+1 */
CGContextRef __nullableCGContextRetain(CGContextRef__nullable c)


/* 图形上下文的引用计数-1. */
void CGContextRelease(CGContextRef__nullable c)


/* 强制所有挂起的绘图操作在一个窗口上下文中立即被渲染到目标设备 */
void CGContextFlush(CGContextRef__nullable c)


/* 将一个窗口的图像上下文内容更新,即所有的绘图操作都会在下次同步到窗口上. */
void CGContextSynchronize(CGContextRef__nullable c)


/** Antialiasing functions. **/

/*
设置该CGContextRef是否应该抗锯齿(即光滑图形曲线边缘)*/
void CGContextSetShouldAntialias(CGContextRef__nullable c,
bool shouldAntialias)

/*
设置该CGContextRef是否允许抗锯齿*/
void CGContextSetAllowsAntialiasing(CGContextRef__nullable c,
bool allowsAntialiasing)

/** Font display functions. **/

/*
设置该CGContextRef是否允许光滑字体*/
void CGContextSetShouldSmoothFonts(CGContextRef__nullable c,
bool shouldSmoothFonts)

/*
设置该CGContextRef是否允许光滑字体*/
void CGContextSetAllowsFontSmoothing(CGContextRef__nullable c,
bool allowsFontSmoothing)

// Enables or disables subpixel positioning in a graphics context.
void CGContextSetShouldSubpixelPositionFonts(
CGContextRef __nullable c, bool shouldSubpixelPositionFonts)


// Sets whether or not to allow subpixel positioning for a graphics context
void CGContextSetAllowsFontSubpixelPositioning(
CGContextRef __nullable c, bool allowsFontSubpixelPositioning)


// Enables or disables subpixel quantization in a graphics context.
void CGContextSetShouldSubpixelQuantizeFonts(
CGContextRef __nullable c, bool shouldSubpixelQuantizeFonts)


// Sets whether or not to allow subpixel quantization for a graphics context
void CGContextSetAllowsFontSubpixelQuantization(
CGContextRef __nullable c, bool allowsFontSubpixelQuantization)


/** Transparency layer support. **/

/* 开始一个透明层。
直到相应的调用CGContextEndTransparencyLayer,在指定范围内的所有后续绘制操作组合到一个完全透明的背景(它被视为一个单独的目标缓冲区从上下文)。

在透明层中绘制需要三步:
1. 调用函数 CGContextBeginTransparencyLayer
2. 在透明层中绘制需要组合的对象
3. 调用函数 CGContextEndTransparencyLayer*/
void CGContextBeginTransparencyLayer(CGContextRef__nullable c,
CFDictionaryRef __nullable auxiliaryInfo)


/* 开始透明度层,它的边界是指定的矩形,其内容是有界的。 */
void CGContextBeginTransparencyLayerWithRect(
CGContextRef __nullable c, CGRect rect, CFDictionaryRef__nullable auxInfo)


/* 结束一个透明层。 */
void CGContextEndTransparencyLayer(CGContextRef__nullable c)

/** User space to device space tranformations. **/

/* 获取Quartz转换用户空间和设备空间的仿射变换 */
CGAffineTransform CGContextGetUserSpaceToDeviceSpaceTransform(CGContextRef__nullable c)

/*———— 点 ————*/
/* 将一个CGPoint数据结构从一个空间变换到另一个空间(DeviceSpace). */
CGPoint CGContextConvertPointToDeviceSpace(CGContextRef__nullable c,
CGPoint point)

/* 将一个CGPoint数据结构从一个空间变换到另一个空间(UserSpace). */
CGPoint CGContextConvertPointToUserSpace(CGContextRef__nullable c,
CGPoint point)

/*———— 大小 ————*/
/* 将一个CGSize数据结构从一个空间变换到另一个空间(DeviceSpace). */
CGSize CGContextConvertSizeToDeviceSpace(CGContextRef__nullable c, CGSize size)


/* 将一个CGSize数据结构从一个空间变换到另一个空间(UserSpace). */
CGSize CGContextConvertSizeToUserSpace(CGContextRef__nullable c, CGSize size)


/*———— 矩形 ————*/
/* 将一个CGPoint数据结构从一个空间变换到另一个空间(DeviceSpace)。 */

CGRect CGContextConvertRectToDeviceSpace(CGContextRef__nullable c,
CGRect rect)

/* 将一个CGPoint数据结构从一个空间变换到另一个空间(UserSpace)。 */
CGRect CGContextConvertRectToUserSpace(CGContextRef__nullable c,
CGRect rect)


/** Deprecated functions. **/
DEPRECATED…
/* 设置在一个图形上下文的字体和字体大小 */
void CGContextSelectFont(CGContextRef__nullable c,
const char *__nullable name, CGFloat size, CGTextEncoding textEncoding)

/* 在当前文本位置,由目前的文本矩阵指定的点显示一个字符数组。 */
void CGContextShowText(CGContextRef__nullable c,
const char *__nullable string, size_t length)

/* 在指定的位置显示一个字符串。 */
void CGContextShowTextAtPoint(CGContextRef__nullable c,
CGFloat x, CGFloat y, const char * __nullable string, size_t length)

/* 在当前文本位置显示一个数组的字形。 */
void CGContextShowGlyphs(CGContextRef__nullable c,
const CGGlyph * __nullable g, size_t count)

/* 在指定的位置显示一个数组的字形。 */
void CGContextShowGlyphsAtPoint(CGContextRef__nullable c, CGFloat x,
CGFloat y, const CGGlyph * __nullable glyphs, size_t count)

/* 绘制具有不同的偏移量的一个数组字形。 */
void CGContextShowGlyphsWithAdvances(CGContextRef__nullable c,
const CGGlyph * __nullable glyphs, const CGSize * __nullable advances,
size_t count)

/* DEPRECATED; use the CGPDFPage API instead. */
void CGContextDrawPDFDocument(CGContextRef__nullable c, CGRect rect, CGPDFDocumentRef__nullable document,int page)
DEPRECATED…