ios画直线原理

画直线方法1:

#pragma mark 画直线-比较简便的画法
void drawLineEasy(){
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextMoveToPoint(ctx, 0, 0);
    CGContextAddLineToPoint(ctx, 100, 100);
    CGContextStrokePath(ctx);
}

画直线方法2:

#pragma mark 画一条直线
void drawLine(){
    
    // 创建图形上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    // 创建路径
    CGMutablePathRef mutpath = CGPathCreateMutable();
    
    // 拼接路径,第二个参数永远都写null
    CGPathMoveToPoint(mutpath, NULL, 0, 0);
    CGPathAddLineToPoint(mutpath, NULL, 100, 100);
    
    // 添加路径到上下文
    CGContextAddPath(ctx, mutpath);
    
    // 渲染
    CGContextStrokePath(ctx);
    
    // 释放内存
    CGPathRelease(mutpath);
}

总结:方法2是方法1代码执行的本质。

posted @ 2014-08-23 18:31  笑看风雨  阅读(528)  评论(0编辑  收藏  举报