H含金量 iOS绘图及贝塞尔曲线关键知识

一、如果是自定义一个继承于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];



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值