2D画图 & CGContextRef 常用的函数和方法

来源:互联网 发布:买汉服哪家店淘宝 编辑:程序博客网 时间:2024/05/22 17:13

// 获取上下文

    CGContextRef context =UIGraphicsGetCurrentContext();

    //保存当前上下文状态

    CGContextSaveGState(context);

    // 得到笔触

    CGContextMoveToPoint(context,200,100);

    //笔触点到另一个点得到一条线

    CGContextAddLineToPoint(context,300,400);

    

    // 线条转弯

    CGContextAddLineToPoint(context,100,500);

    

    // 设置笔触颜色

    CGContextSetStrokeColorWithColor(context, [UIColorblackColor].CGColor);

    

    // 设置线宽度

    CGContextSetLineWidth(context,5);

    

    // 设置join出样式

    CGContextSetLineJoin(context,kCGLineJoinBevel);

    

    //设置线末尾 lineCap

    CGContextSetLineCap(context,kCGLineCapRound);

    

    //画椭圆 其实画的是矩形的内切椭圆第一个点表示x,y坐标第二个点表示宽高

    CGContextAddEllipseInRect(context,CGRectMake(50,50,50, 100));

    // 画圆 同上

    CGContextAddEllipseInRect(context,CGRectMake(50,200,100, 100));

    CGContextAddEllipseInRect(context,CGRectMake(75,200,50, 100));

    CGContextAddEllipseInRect(context,CGRectMake(50,220,100, 50));

    //封闭区域的填充所有能够构成闭合区域的甚至是折线

//    CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor);

    //设置填充模式默认填充色是黑色

    CGContextDrawPath(context,kCGPathEOFillStroke);

    //显示画图信息构思后的具体样式

    //填充封闭区域只要能够构成封闭的区域

    CGContextFillPath(context);

    // 只是描线

//    CGContextStrokePath(context);

    

    //获取先前保存的上下文状态

    CGContextRestoreGState(context);

    //在新的画布上重新绘图相当于构思

    CGContextMoveToPoint(context,300,100);

    CGContextAddLineToPoint(context,300,200);

    //显示画图信息构思后的具体样式

    CGContextDrawPath(context,kCGPathStroke);

-------------------------------------------------------------------------------

  //通过path来画图

 // 获取开始是context的状态

    CGContextRestoreGState(context);

    

    // 起始笔触点

    CGPathMoveToPoint(_path,NULL, 0, 0);

    CGPathAddLineToPoint(_path,NULL, 100, 100);

    CGPathAddLineToPoint(_path,NULL, 0, 100);

    

    //画椭圆  其实画的是矩形的内切椭圆第一个点表示x,y 坐标第二个点表示 宽高

    CGContextAddEllipseInRect(context,CGRectMake(50,50, 50, 100));

    // 画圆 同上

    CGContextAddEllipseInRect(context,CGRectMake(50,200, 100, 100));

    CGContextAddEllipseInRect(context,CGRectMake(75,200, 50, 100));

    CGContextAddEllipseInRect(context,CGRectMake(50,220, 100, 50));

    CGContextSetFillColorWithColor(context, [UIColoryellowColor].CGColor);

    

    // 将描述好的path加入到context

    CGContextAddPath(context, _path);

    // 绘制path 包括可以设置填充方式

    CGContextDrawPath(context,kCGPathEOFillStroke);






0 0
原创粉丝点击