H含金量 iOS绘图及贝塞尔曲线关键知识
2016-07-06 14:56
423 查看
一、如果是自定义一个继承于UIView的子类
要在这个子类view上画图的话:
1.假如是在drawRect:方法中单纯地画贝塞尔曲线
// Set the color: Sets the fill and stroke colors in the current drawing context. Should be implemented by subclassers.
- (void)set;
// Set the fill or stroke colors individually. These should be implemented by subclassers.
- (void)setFill;
- (void)setStroke;
这三个方法是用来设置这种画线方式所需的边色或者填充色(是类UIColor的实例方法)
UIBezierPath* aPath = [UIBezierPathbezierPath];
[aPath stroke];//这个方法必须要写的 否则不会画出来(描边)否则不会画线
[aPath fill];(填充)
2.假如画完线之后(不论是否在drawRect:方法中),所画贝塞尔曲线的CGPath属性是用作CAShapeLayer的path属性的
那么[aPath stroke]、[aPath
fill]这两个方法可以不要,
(1)如果非要写的话画线的代码需要放到
UIGraphicsBeginImageContext(self.view.bounds.size);
UIGraphicsEndImageContext();这两句代码之间才能避免控制台打印出一串
“
Jul 6 14:32:00 TextBeiSaiEr[7433] <Error>: CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental
variable.
Jul 6 14:32:00 TextBeiSaiEr[7433] <Error>: CGContextSetLineWidth: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Jul 6 14:32:00 TextBeiSaiEr[7433] <Error>: CGContextSetLineJoin: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Jul 6 14:32:00 TextBeiSaiEr[7433] <Error>: CGContextSetLineCap: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Jul 6 14:32:00 TextBeiSaiEr[7433] <Error>: CGContextSetMiterLimit: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Jul 6 14:32:00 TextBeiSaiEr[7433] <Error>: CGContextSetFlatness: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Jul 6 14:32:00 TextBeiSaiEr[7433] <Error>: CGContextAddPath: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Jul 6 14:32:00 TextBeiSaiEr[7433] <Error>: CGContextDrawPath: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Jul 6 14:32:00 TextBeiSaiEr[7433] <Error>: CGContextRestoreGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
”
这些警告。
如果在这两句代码之间要用
CGContextRef con =UIGraphicsGetCurrentContext();
CGContextAddEllipseInRect(con,
CGRectMake(0,0,100,100));
CGContextSetFillColorWithColor(con, [UIColorblueColor].CGColor);
CGContextFillPath(con);
这些代码来画线的话,下面这句代码是必须的
UIImage* im =
UIGraphicsGetImageFromCurrentImageContext();
(2)如果不写[aPath stroke]、[aPath
fill]这两个方法的话
UIGraphicsBeginImageContext(self.view.bounds.size);
UIGraphicsEndImageContext();
这两句代码不需要
二、如果不自定义UIView的子类
可以直接按一中的第2点来处理
代码如下:(写在viewDidLoad方法中的)
UIBezierPath* aPath = [UIBezierPathbezierPath];
aPath.lineWidth =5.0;
aPath.lineCapStyle =kCGLineCapRound;
//线条拐角
aPath.lineJoinStyle =kCGLineCapRound;
//终点处理
// Set the starting point of the shape.
[aPath moveToPoint:CGPointMake(100.0,20.0)];
// Draw the lines
[aPath addLineToPoint:CGPointMake(200.0,40.0)];
[aPath addLineToPoint:CGPointMake(160,140)];
[aPath addLineToPoint:CGPointMake(40.0,140)];
[aPath addLineToPoint:CGPointMake(0.0,40.0)];
[aPath closePath];//第五条线通过调用closePath方法得到的
CAShapeLayer * slayer = [CAShapeLayerlayer];
slayer.path = aPath.CGPath;
slayer.backgroundColor = [UIColorclearColor].CGColor;
slayer.fillColor = [UIColorclearColor].CGColor;
slayer.lineCap =kCALineCapRound;
slayer.lineJoin =kCALineJoinRound;
slayer.strokeColor = [UIColorredColor].CGColor;
slayer.lineWidth = aPath.lineWidth;
[self.view.layeraddSublayer:slayer];
要在这个子类view上画图的话:
1.假如是在drawRect:方法中单纯地画贝塞尔曲线
// Set the color: Sets the fill and stroke colors in the current drawing context. Should be implemented by subclassers.
- (void)set;
// Set the fill or stroke colors individually. These should be implemented by subclassers.
- (void)setFill;
- (void)setStroke;
这三个方法是用来设置这种画线方式所需的边色或者填充色(是类UIColor的实例方法)
UIBezierPath* aPath = [UIBezierPathbezierPath];
[aPath stroke];//这个方法必须要写的 否则不会画出来(描边)否则不会画线
[aPath fill];(填充)
2.假如画完线之后(不论是否在drawRect:方法中),所画贝塞尔曲线的CGPath属性是用作CAShapeLayer的path属性的
那么[aPath stroke]、[aPath
fill]这两个方法可以不要,
(1)如果非要写的话画线的代码需要放到
UIGraphicsBeginImageContext(self.view.bounds.size);
UIGraphicsEndImageContext();这两句代码之间才能避免控制台打印出一串
“
Jul 6 14:32:00 TextBeiSaiEr[7433] <Error>: CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental
variable.
Jul 6 14:32:00 TextBeiSaiEr[7433] <Error>: CGContextSetLineWidth: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Jul 6 14:32:00 TextBeiSaiEr[7433] <Error>: CGContextSetLineJoin: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Jul 6 14:32:00 TextBeiSaiEr[7433] <Error>: CGContextSetLineCap: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Jul 6 14:32:00 TextBeiSaiEr[7433] <Error>: CGContextSetMiterLimit: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Jul 6 14:32:00 TextBeiSaiEr[7433] <Error>: CGContextSetFlatness: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Jul 6 14:32:00 TextBeiSaiEr[7433] <Error>: CGContextAddPath: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Jul 6 14:32:00 TextBeiSaiEr[7433] <Error>: CGContextDrawPath: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Jul 6 14:32:00 TextBeiSaiEr[7433] <Error>: CGContextRestoreGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
”
这些警告。
如果在这两句代码之间要用
CGContextRef con =UIGraphicsGetCurrentContext();
CGContextAddEllipseInRect(con,
CGRectMake(0,0,100,100));
CGContextSetFillColorWithColor(con, [UIColorblueColor].CGColor);
CGContextFillPath(con);
这些代码来画线的话,下面这句代码是必须的
UIImage* im =
UIGraphicsGetImageFromCurrentImageContext();
(2)如果不写[aPath stroke]、[aPath
fill]这两个方法的话
UIGraphicsBeginImageContext(self.view.bounds.size);
UIGraphicsEndImageContext();
这两句代码不需要
二、如果不自定义UIView的子类
可以直接按一中的第2点来处理
代码如下:(写在viewDidLoad方法中的)
UIBezierPath* aPath = [UIBezierPathbezierPath];
aPath.lineWidth =5.0;
aPath.lineCapStyle =kCGLineCapRound;
//线条拐角
aPath.lineJoinStyle =kCGLineCapRound;
//终点处理
// Set the starting point of the shape.
[aPath moveToPoint:CGPointMake(100.0,20.0)];
// Draw the lines
[aPath addLineToPoint:CGPointMake(200.0,40.0)];
[aPath addLineToPoint:CGPointMake(160,140)];
[aPath addLineToPoint:CGPointMake(40.0,140)];
[aPath addLineToPoint:CGPointMake(0.0,40.0)];
[aPath closePath];//第五条线通过调用closePath方法得到的
CAShapeLayer * slayer = [CAShapeLayerlayer];
slayer.path = aPath.CGPath;
slayer.backgroundColor = [UIColorclearColor].CGColor;
slayer.fillColor = [UIColorclearColor].CGColor;
slayer.lineCap =kCALineCapRound;
slayer.lineJoin =kCALineJoinRound;
slayer.strokeColor = [UIColorredColor].CGColor;
slayer.lineWidth = aPath.lineWidth;
[self.view.layeraddSublayer:slayer];
相关文章推荐
- iOS开发问题----去掉输入键盘上的自动联想部分,限制长度内容,以及禁止输入emoji表情等问题
- presentViewController跳转动画切换
- [iOS] iOS开发的22个奇葩技巧
- ios 健康HealthKit的数据采样
- iOS 截屏
- iOS图片压缩
- iOS开发——Watch开发环境设置
- iOS中图片压缩的常见方法
- IOS-抽屉效果
- iOS实现头像选取(照相或者图片库)、大小等比缩放、生成圆形头像 (转)
- iOS 给文字上面加贯穿横线
- iOS开发经验相关知识
- iOS开发经验相关知识
- 多任务
- iOS巅峰之根据图片链接生成图片
- 学习方法
- IOS 设置百度地图自定义标注图片,自定义泡泡
- IOS使用ijkPlayer(转)
- iOS-Echarts动态图表控件的使用
- 一步一步拆解一个简单的iOS轮播图(三图)