iOS学习笔记28-Quartz2D

Quartz 2D是一个二维绘图引擎,Quartz 2D 是纯C语言,来自于core graphics框架,没有面向对象的思想

1,作用:绘制图形:线条\三角形\矩形\圆形\弧等

 

绘制文字,绘制生成图片,读取声称PDF,裁剪图片,自定义UI控件

 

2,图形上下文

作用:保存绘图信息,绘图状态

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

 

 

图形上下文栈

将上下文拷贝一份放到栈中

入栈

CGContextSaveGState(context)

出栈

CGContextRestoreGState(context)

 

 

 

在UIView中绘图只能在DrawRect方法中绘图

 

 

创建一个UIView 对象

放到文件夹下 选中SB 添加一个UIView 改为自定义的drawline

 

 

首先绘制线条

void drawLine(){

//1,获得图形上下文

    CGContextRef context = UIGraphicsGetCurrentContext();

//2,绘制图形

    //线段宽度

    CGContextSetLineWidth(context, 2);

    //线条的颜色

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

    //设置起点

    CGContextMoveToPoint(context, 10, 10);

    //画线

    CGContextAddLineToPoint(context, 100, 100);

//3,显示到View

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

 

 

 

然后绘制两条线

   

    /**

     *两条线

     **/

    [[UIColor redColor]set];

    //线条头尾部的样式

    CGContextSetLineCap(context, kCGLineCapRound);

    //线条转折点的样式

    CGContextSetLineJoin(context, kCGLineJoinRound);

    

    //画线

    CGContextMoveToPoint(context, 100, 120);

    CGContextAddLineToPoint(context, 150, 120);

    CGContextAddLineToPoint(context, 150, 100);

    

    

    //显示到View

    CGContextStrokePath(context);

 

 

 

 

 

然后绘制 矩形

 

void drawRect(){

//1,获得上下文

    CGContextRef context = UIGraphicsGetCurrentContext();

    //绘制四边形

    CGContextAddRect(context, CGRectMake(10, 10, 120, 180));

    [[UIColor yellowColor]setFill];

    CGContextFillPath(context);

 

}

 

 

 

 

最后绘制三角形

 

void drawTriangle(){

    CGContextRef context = UIGraphicsGetCurrentContext();

    

    CGContextMoveToPoint(context, 0, 0);

    CGContextAddLineToPoint(context, 100, 100);

    CGContextAddLineToPoint(context, 150, 100);

    //关闭路径 链接起点和最后一个点

    CGContextClosePath(context);

    

    CGContextStrokePath(context);

    

}

 

 

 

 

- (void)drawRect:(CGRect)rect {

 

//    drawCircle();

//    drawArc();

    

//    drawText();

//    drawImg();

    drawBezier();

}

 

 

 

void drawCircle(){

    CGContextRef context = UIGraphicsGetCurrentContext();

    

    //绘制图形

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

    CGContextStrokePath(context);

 

}

 

void drawArc(){

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextAddArc(context, 100, 100, 50, M_PI_4, M_PI_2, 1);

    [[UIColor redColor]set];

    CGContextSetLineJoin(context, kCGLineJoinRound);

    CGContextSetLineCap(context, kCGLineCapRound);

    CGContextSetLineWidth(context, 10);

 

    CGContextStrokePath(context);

    CGContextFillPath(context);

}

 

void drawText(){

    NSString *str = @"哈哈哈哈哈哈哈";

    NSMutableDictionary *atrributes = [[NSMutableDictionary alloc]init];

    atrributes[NSFontAttributeName] = [UIFont systemFontOfSize:20];

    atrributes[NSForegroundColorAttributeName] = [UIColor blueColor];

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

 

 

}

 

void drawImg(){

 

    UIImage *img = [UIImage imageNamed:@"1.jpg"];

//    

//    [img drawAtPoint:CGPointMake(50, 50)];

    [img drawInRect:CGRectMake(0, 0, 300, 300)];

//    [img drawAsPatternInRect:CGRectMake(0, 0, 300, 300)];

    

    NSString *str = @"中国移动?";

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

}

 

//贝塞尔曲线

void drawBezier(){

 

    CGContextRef context = UIGraphicsGetCurrentContext();

    

    CGContextMoveToPoint(context, 10, 10);

    //2个控制点

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

    //2个控制点

    CGContextAddQuadCurveToPoint(context, 120, 100, 180, 190);

    CGContextSetLineWidth(context, 20);

    CGContextSetLineJoin(context, kCGLineJoinRound);

    [[UIColor redColor]set];

    CGContextStrokePath(context);

 

 

}

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/adodo/p/5222368.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值