Quartz 2D绘画直线,四边形,三角形,圆,圆弧的基本方法

来源:互联网 发布:陕西省科协网络平台 编辑:程序博客网 时间:2024/05/24 05:03

首先介绍下Quartz 2D,Quartz 2D是一个二维绘图引擎。Quartz 2D的API是C语言,来自于CoreGraphics框架。没有面相对象的思想。

1.作用:

绘制图形;线条,三角形,矩形,圆,圆弧等。

绘制文字

绘制,生成图片,图像

读取,生成PDF

截图,裁减图片

自定义UI控件

2.图形上下文(Graphics Context):是一个CGContextRef类型的数据。

图形上下文的作用:A.保存绘图信息,状态;

B.决定绘制的输出目标(绘制到什么地方去,输出目标可以是PDF文件。Bitmap或者显示器的窗口上)

在UIView中绘图只能在DrawRect方法中获得图形上下文,并绘图。drawRect系统调用,不能手动调用。视图显示在屏幕上的时候调用,且只调用一次。

绘制直线,四边形,三角形

// 系统自动调用

- (void)drawRect:(CGRect)rect {

    // Drawing code

    [selfdrawLine];

    //[self drawR];

    //[self drawTriangle];

}

 

//画线

- (void) drawLine

{

    //1.获得图形上下文

    CGContextRef context = UIGraphicsGetCurrentContext();

     

    //将上下文复制一份到栈中

    CGContextSaveGState(context);

     

    //2.绘制图形

    //第一条线

    //设置线段宽度

    CGContextSetLineWidth(context, 20);

    //设置线条头尾部的样式

    CGContextSetLineCap(context, kCGLineCapRound);

     

    //设置颜色

    CGContextSetRGBStrokeColor(context, 1, 0, 0, 1);

    //设置起点

    CGContextMoveToPoint(context, 10, 10);

    //画线

    CGContextAddLineToPoint(context, 100, 100);

     

    //3.显示到View

    CGContextStrokePath(context);//以空心的方式画出

     

    //将图形上下文出栈,替换当前的上下文

    CGContextRestoreGState(context);

     

    [[UIColor blueColor] set];

     

    //设置线段转折点的样式

    CGContextSetLineJoin(context, kCGLineJoinRound);

    //画线

    CGContextMoveToPoint(context, 100, 120);

     

    CGContextAddLineToPoint(context, 150, 120);

    CGContextAddLineToPoint(context, 150, 180);

     

    //3.显示到view

    CGContextStrokePath(context);

}



//绘制四边形

- (void) drawR

{

    //1.获得图形上下文

    CGContextRef context = UIGraphicsGetCurrentContext();

    //2.绘制四边形

    CGContextAddRect(context, CGRectMake(20, 20, 100, 100));

    //设置颜色

    [[UIColor purpleColor] setFill];

    //3.显示在view上

    CGContextFillPath(context);

}



//绘制三角形

- (void) drawTriangle

{

    //1.获得图形上下文

    CGContextRef context = UIGraphicsGetCurrentContext();

    //绘制三角形

    CGContextMoveToPoint(context, 50, 0);

    CGContextAddLineToPoint(context, 100, 100);

    CGContextAddLineToPoint(context, 0, 100);

    //关闭路径,闭环,(连接起点和最后一个点)

    CGContextSetLineWidth(context, 2);

    CGContextClosePath(context);

    [[UIColor whiteColor] set];

    

    //显示在view上

    CGContextStrokePath(context);

}



绘制圆,圆弧,贝塞尔曲线,文字,图片

- (void)drawRect:(CGRect)rect {

    // Drawing code

    //drawCircle();

    drawArc();

    //drawText();

    //drawImg();

    //drawBezier();

}



//画圆弧

void drawArc()

{

    //1.获得图形上下文

    CGContextRef context = UIGraphicsGetCurrentContext();

    //2.绘制图形

    CGContextAddArc(context, 100, 100, 50, arc(90), arc(30), 0);

    //CGContextAddArc(context, 100, 100, 50, M_PI_2, M_PI, 0);

    //                                      90,    180, 1逆时针,0顺时针

    //3.显示

    CGContextStrokePath(context);

}



//绘制文字

void drawText()

{

    NSString *str = @"测试文本";

    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];

    attributes[NSFontAttributeName] = [UIFont systemFontOfSize:20];//设置文字大小

    attributes[NSForegroundColorAttributeName] = [UIColor purpleColor];

    [str drawInRect:CGRectMake(100, 100, 100, 30) withAttributes:attributes];

    

}



//画图片

void drawImg()

{

    //1.取得图片

    UIImage *img = [UIImage imageNamed:@"120"];

    //2.画

    //[img drawAtPoint:CGPointMake(20, 20)];//在(20,20)这个位置开始画

    

    //[img drawInRect:CGRectMake(20, 20, 100, 100)];//设置起始点,和宽高,会自动拉伸图片

    

    [img drawAsPatternInRect:CGRectMake(0, 0, 190, 190)];//不会自动拉伸图片,不够时会自动平铺,类似于格子

    

    //增加文字,也可以设置水印

    NSString *str = @"测试文本";

    [str drawInRect:CGRectMake(0, 0, 100, 30) withAttributes:nil];

}



//贝塞尔曲线

void drawBezier()

{

    //1.取得图形上下文

    CGContextRef context = UIGraphicsGetCurrentContext();

    //起点

    CGContextMoveToPoint(context, 10, 10);

    //2个控制点

    //CGContextAddCurveToPoint(context, 120, 100,   180, 50,  10, 190);

    //                               第一个控制点,第二个控制点,   终点

     

    //1个控制点

    CGContextAddQuadCurveToPoint(context, 150, 200, 200, 100);

     

    CGContextStrokePath(context);

}



0 0
原创粉丝点击