ios-绘图的方式

来源:互联网 发布:软件社区 编辑:程序博客网 时间:2024/06/05 16:04

以下方法都是在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];






原创粉丝点击