XMG UIBezierPath与CGContextRef
2016-05-08 18:01
507 查看
1.
贝泽尔路径只有当stroke的时候才会添加到上下文当中
如果想要在stroke之前就添加到上下文中的话
// 把路径添加到上下文
// .CGPath
可以UIkit的路径转换成CoreGraphics路径
CGContextAddPath(ctx, path.CGPath);
如果用贝泽尔stroke 的话只认贝泽尔的状态,是不去管上下文的状态
2.
现在存在的问题是如果我设置的是上下文的状态的话。那么以后绘制的内容所有的状态都和现在一样。
我还需要去单独进行设置
3.
绘图的状态包括线宽和颜色
上下文对象可以理解为内存缓冲区,内容如果直接渲染的话效率比较低。先放到上下文上面,然后再进行渲染的话,可以提高效率
可以保存一份上下文的状态‘
然后下一次的时候恢复上下文的状态
UIBezierPath 尽量不要与CGContextRef 混着用,因为贝泽尔只有Stroke的时候才会添加到上下文对象上面。通过上下文如果想描绘之前控制贝泽尔的话,需要将它添加进来
源码
#import "DrawView.h"
@implementation DrawView
// 如果以后用贝瑟尔绘制图形【path stroke】,上下文的状态由贝瑟尔路径状态
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
// 1.获取上下文
CGContextRef ctx =
UIGraphicsGetCurrentContext();
// 2.描述路径
//
第一根
UIBezierPath *path = [UIBezierPath
bezierPath];
[path moveToPoint:CGPointMake(10,
125)];
[path addLineToPoint:CGPointMake(240,
125)];
//
把路径添加到上下文
// .CGPath
可以UIkit的路径转换成CoreGraphics路径
CGContextAddPath(ctx, path.CGPath);
//
保存一份上下文的状态
CGContextSaveGState(ctx);
//
设置上下文状态
CGContextSetLineWidth(ctx,
10);
[[UIColor
redColor] set];
//
渲染上下文
CGContextStrokePath(ctx);
//
第二根
// 2.描述路径
//
第一根
path = [UIBezierPath
bezierPath];
[path moveToPoint:CGPointMake(125,
10)];
[path addLineToPoint:CGPointMake(125,
240)];
//
把路径添加到上下文
// .CGPath
可以UIkit的路径转换成CoreGraphics路径
CGContextAddPath(ctx, path.CGPath);
//
还原状态
CGContextRestoreGState(ctx);
// // 设置上下文状态
// CGContextSetLineWidth(ctx, 1);
//
// [[UIColor blackColor] set];
//
渲染上下文
CGContextStrokePath(ctx);
}
贝泽尔路径只有当stroke的时候才会添加到上下文当中
如果想要在stroke之前就添加到上下文中的话
// 把路径添加到上下文
// .CGPath
可以UIkit的路径转换成CoreGraphics路径
CGContextAddPath(ctx, path.CGPath);
如果用贝泽尔stroke 的话只认贝泽尔的状态,是不去管上下文的状态
2.
现在存在的问题是如果我设置的是上下文的状态的话。那么以后绘制的内容所有的状态都和现在一样。
我还需要去单独进行设置
3.
绘图的状态包括线宽和颜色
上下文对象可以理解为内存缓冲区,内容如果直接渲染的话效率比较低。先放到上下文上面,然后再进行渲染的话,可以提高效率
可以保存一份上下文的状态‘
然后下一次的时候恢复上下文的状态
UIBezierPath 尽量不要与CGContextRef 混着用,因为贝泽尔只有Stroke的时候才会添加到上下文对象上面。通过上下文如果想描绘之前控制贝泽尔的话,需要将它添加进来
源码
#import "DrawView.h"
@implementation DrawView
// 如果以后用贝瑟尔绘制图形【path stroke】,上下文的状态由贝瑟尔路径状态
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
// 1.获取上下文
CGContextRef ctx =
UIGraphicsGetCurrentContext();
// 2.描述路径
//
第一根
UIBezierPath *path = [UIBezierPath
bezierPath];
[path moveToPoint:CGPointMake(10,
125)];
[path addLineToPoint:CGPointMake(240,
125)];
//
把路径添加到上下文
// .CGPath
可以UIkit的路径转换成CoreGraphics路径
CGContextAddPath(ctx, path.CGPath);
//
保存一份上下文的状态
CGContextSaveGState(ctx);
//
设置上下文状态
CGContextSetLineWidth(ctx,
10);
[[UIColor
redColor] set];
//
渲染上下文
CGContextStrokePath(ctx);
//
第二根
// 2.描述路径
//
第一根
path = [UIBezierPath
bezierPath];
[path moveToPoint:CGPointMake(125,
10)];
[path addLineToPoint:CGPointMake(125,
240)];
//
把路径添加到上下文
// .CGPath
可以UIkit的路径转换成CoreGraphics路径
CGContextAddPath(ctx, path.CGPath);
//
还原状态
CGContextRestoreGState(ctx);
// // 设置上下文状态
// CGContextSetLineWidth(ctx, 1);
//
// [[UIColor blackColor] set];
//
渲染上下文
CGContextStrokePath(ctx);
}
相关文章推荐
- Android Volley完全解析(三),定制自己的Request
- Android之十二微信UI界面设计
- UID卡修改&UID锁死修复
- Android高级UI之自定义复合控件--标题栏
- bzoj 1367: [Baltic2004]sequence(中位数+可并堆)
- Error getting nested result map values for 'user_inf'. Cause: java.sql.SQLException: Cannot convert
- 百度编辑器ueditor的简单使用
- paper:synthesizable finit state machine design techniques using the new systemverilog 3.0 enhancements之output encoded style with registered outputs(Good style)
- UE4:关闭自动曝光
- mapreduece和Hadoop啥关系
- UI进阶 数据库 SQLite
- 300. Longest Increasing Subsequence 最长子序列
- BSCMAKE: error BK1513 : nonincremental update requires all .SBR files
- UESTC--1041--Hug the princess(位运算)
- 阿里巴巴Druid连接池配置
- 语法篇5之关键词void、break、continue、new
- easyUi 修改页面
- easyUi 新增页面
- easyUI 增删改
- subarray、subsequence的区别