Quartz2D总结
- 作者: 五速梦信息网
- 时间: 2026年04月04日 13:30
天了噜,脑子完全懵了,最起码说出来个上下文啊,连这个都给忘了,特此总结一下,并以此缅怀这次面试
Quartz2D的API来自于Core Graphics(这就是为什么CGContextRef是以CG开头),需要导入CoreGraphics(Xcode6之后不需要导入了)
图形上下文:是一个CGContextRef类型的数据
图形上下文的作用:
1.保存绘图信息、绘图状态
2.决定绘制的输出目标(绘制到什么地方去)(输出目标可以是PDF文件、Bitmap或者显示器的窗口上
Bitmap Graphics Context
PDF Graphics Context
Window Graphics Context
Layer Graphics Context
Printer Graphics Context
自己画一个view
步骤:
1.新建一个类,继承自UIView
2.实现- (void)drawRect:(CGRect)rect方法,并在此方法中取得当前view的图形上下文
3.创建自己的绘制path,并根据path来绘制图形
4.利用图形上下文将绘制的所有内容渲染显示到view上面 当然,比如Bitmap的图形上下文就不必在- (void)drawRect:(CGRect)rect里面了,直接创建一个就可:
// 创建一个bitmap上下文
UIGraphicsBeginImageContextWithOptions(CGSizeMake(, ), NO, );
// 获取bitmap上下文
CGContextRef ctr = UIGraphicsGetCurrentContext();
// 画圆
CGContextAddEllipseInRect(ctr, CGRectMake(, , , ));
// 渲染
CGContextStrokePath(ctr);
// 从上下文获取画出的图片
UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); UIImageView *imageView = [[UIImageView alloc] initWithImage:img];
CGRect frame = imageView.frame;
frame.origin = CGPointMake(, );
imageView.frame = frame;
[self.view addSubview:imageView];
待续。。。
部分参考自:http://www.jianshu.com/p/eecffec3b7af
- 上一篇: querylist4 thinkphp5 采集并保存
- 下一篇: quartus无法转modelsim
相关文章
-
querylist4 thinkphp5 采集并保存
querylist4 thinkphp5 采集并保存
- 互联网
- 2026年04月04日
-
questa UVM版本
questa UVM版本
- 互联网
- 2026年04月04日
-
Questions and Answers
Questions and Answers
- 互联网
- 2026年04月04日
-
quartus无法转modelsim
quartus无法转modelsim
- 互联网
- 2026年04月04日
-
QT自定义精美换肤界面
QT自定义精美换肤界面
- 互联网
- 2026年04月04日
-
Qt自定义滚动条(不使用样式表)
Qt自定义滚动条(不使用样式表)
- 互联网
- 2026年04月04日






