ios-绘图的方式

以下方法都是在drawRect方法中调用的

纯C的方式1

//获取当前绘图上下文
    CGContextRef ctx=UIGraphicsGetCurrentContext();
    //拼接路径 同时把路径添加到上下文中
    CGContextMoveToPoint(ctx, 50, 50);//到这里的时候相当于你笔只是停留在这个位置的上面
    CGContextAddLineToPoint(ctx, 100, 100);//已经开始画了笔还没有抬起来
    //描边渲染,上下文还在上下文的路径已经拖给了UIView了
    CGContextStrokePath(ctx);
纯C的方式2
//1、获取当前绘图上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //2、拼接路径,这个是可变路径
    CGMutablePathRef path=CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 50, 50);
    CGPathAddLineToPoint(path, NULL, 100, 100);
    //3、把路径添加到上下文当中,这个路径是复制出来的也就是说你线在外面先画好,在复制到上下文中
    CGContextAddPath(ctx, path);
    //4、渲染
    CGContextStrokePath(ctx);
    //释放
    CGPathRelease(path); 
    //也可以这样
    CFRelease(path); 
C+OC的方式1
  //1、获取当前上下文
    CGContextRef ctx=UIGraphicsGetCurrentContext();
    //2、拼接路径(oc的方式)
    UIBezierPath * path=[[UIBezierPath alloc]init];
    [path moveToPoint:CGPointMake(50, 50)];
    [path addLineToPoint:CGPointMake(100, 100)];
    //3、把路径添加到上下文当中(oc中的path转成c的path就是加个.CGpath)
    CGContextAddPath(ctx, path.CGPath);
    //4、渲染
    CGContextStrokePath(ctx);

C+OC的方式2

 //1、获取当前上下文
    CGContextRef ctx=UIGraphicsGetCurrentContext();
      //2、拼接路径(c)
    CGMutablePathRef path=CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 50, 50);
    CGPathAddLineToPoint(path, NULL, 100, 100);
      //3、拼接路径,接着上面停下来的那个点接着画线,这里把C的path转换成OC的path
    UIBezierPath * path1=[UIBezierPath bezierPathWithCGPath:path];
    [path1 addLineToPoint:CGPointMake(150, 50)];
    //4、把路径添加到上下文当中
    CGContextAddPath(ctx, path1.CGPath);
    //5、渲染
    CGContextStrokePath(ctx);
纯OC的方式

 UIBezierPath * path=[UIBezierPath bezierPath];
    //拼接路径
    [path moveToPoint:CGPointMake(50, 50)];
    [path addLineToPoint:CGPointMake(100, 100)];
    //渲染,在这里就会自动的帮你获取图形上下文了,帮你把路径添加到上下文当中
    [path stroke];






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值