Quartz 2D绘图基本用法

 1.获取上下文
 2.设置路径(画的形状)
 3.将路径添加到上下文
 4.设置上下文属性(画笔的粗细、颜色、阴影等)
 5.绘制路径
 6.释放路径(某些情况)
  //1.取得上下文(画布)
    CGContextRef context = UIGraphicsGetCurrentContext();

    //绘制线条
//    [self drawLine:context];
//    [self drawLine2:context];

    //绘制矩形
//    [self drawShapeRect:context];

    //绘制圆
//    [self drawArc:context];

    //绘制贝塞尔曲线
//    [self drawCure:context];

    //绘制文字
//    [self drawText:context];

    // 绘制图像
    [self drawImg:context];
}
#pragma mark - 绘制图像
- (void)drawImg:(CGContextRef)context {

    UIImage *img = [UIImage imageNamed:@"2012100413195471481.jpg"];
    //UIKit中提供的绘图方式
    //  1> 指定绘制图像的点,无法设置图片的大小
//    [img drawAtPoint:CGPointMake(50, 50)];
    // 2> 在指定的区域绘制,图片会被拉伸
//    [img drawInRect:CGRectMake(0, 0, 320, 200)];
    // 3> 在指定区域绘制图片,平铺
//    [img drawAsPatternInRect:CGRectMake(0, 0, 320, 200)];

    //Core Graphic中的绘图方式

    //保存context
    CGContextSaveGState(context);

    //转换坐标
    //顺时针旋转180
//    CGContextRotateCTM(context, M_PI);
    //缩放
    CGContextScaleCTM(context, 1, -1);
    //平移
    CGContextTranslateCTM(context, 0, -200);

    CGContextDrawImage(context, CGRectMake(0, 0, 320, 200), img.CGImage);

    //回复上下文
    CGContextRestoreGState(context);

}

#pragma mark - 绘制文字
- (void)drawText:(CGContextRef) context {

    //如果相对文字设置有特殊的要求,可以用:Core Text  底层:C

//    NSString *string = @"无限互联3G学院";
    NSString *string = @"hello world hello world hello world hello world hello world";

    CGRect rect = CGRectMake(50, 50, 200, 200);

    //设置颜色
    [[UIColor redColor] setFill];

    //绘制矩形
    UIRectFill(rect);

    //设置字体的颜色
    [[UIColor greenColor] setFill];

    UIFont *font = [UIFont systemFontOfSize:20];

    //绘制文字
    [string drawInRect:rect withFont:font lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentCenter];

}

#pragma mark - 绘制贝塞尔曲线
- (void)drawCure:(CGContextRef) context{

    //1.设置起始点
    CGContextMoveToPoint(context, 20, 200);

    /*
     context:  绘制的上下文
     cp1x,cp1y:  第一条切线的终点
     cp2x,cp2y;  第二条切线的起点
     x,y   第二条切线的终点
     */
//    CGContextAddCurveToPoint(context, 100, 20,
//                                      200, 300,
//                                      300, 50);

    CGContextAddQuadCurveToPoint(context, 140, 20, 300, 200);

    //绘制
    CGContextDrawPath(context, kCGPathStroke);


}

#pragma mark - 绘制圆弧
- (void)drawArc:(CGContextRef)context {

    //1.绘制圆弧
    /*
     context:  上下文
     x,y:       圆心坐标
     radius:    半径
     startAngle:  起始角度
     endAngle:  结束的角度
     clockwise:  顺时针: 0   逆时针  1
    CGContextAddArc(context, 160, 100, 100 , 0, M_PI/4, 1);

    //设置颜色
    [[UIColor redColor] setFill];

    //绘制
    CGContextDrawPath(context, kCGPathStroke);
    */

    //2.利用矩形绘制圆
    CGRect rect = CGRectMake(50, 50, 200, 200);

    //设置颜色
    [[UIColor redColor] setStroke];
    [[UIColor greenColor] setFill];

    //绘制矩形
    UIRectFrame(rect);

    //绘制矩形的内切圆
    CGContextAddEllipseInRect(context, rect);
    //绘制
    CGContextDrawPath(context, kCGPathFillStroke);

}

#pragma mark - 绘制矩形
- (void)drawShapeRect:(CGContextRef)context {

/*---------------------1.Graphic提供的方法------------------------------*/

//    //1.获取上下文
//   
//    //2.绘制矩形
//    CGRect rect = CGRectMake(40, 40, 100, 200);
//    CGContextAddRect(context, rect);
//   
//    //3.设置线条的宽度和颜色
//    CGContextSetLineWidth(context, 5);
//    [[UIColor redColor] setStroke];
//    [[UIColor blueColor] setFill];
//   
//    //4.绘制
//    CGContextDrawPath(context, kCGPathFillStroke);

/*---------------------UIKit提供的方法------------------------------*/


    //设置颜色,注意:得在绘图之前设置
    [[UIColor redColor] setStroke];
    [[UIColor greenColor] setFill];

    CGRect rect = CGRectMake(40, 40, 100, 200);
    //实心的矩形
    UIRectFill(rect);
    //空心的矩形
    UIRectFrame(rect);

}

#pragma mark - 绘制线条2
- (void)drawLine2:(CGContextRef)context {

    //1.取得上下文

    //2.添加多条线
    CGPoint p0 = {50,50};
    CGPoint p1 = {200,200};
    CGPoint p2 = {50,200};
    CGPoint p3 = {50,50};
    CGPoint points[] = {p0,p1,p2,p3};
//    int a[3];
    CGContextAddLines(context, points, 4);

    //UIKit中设置颜色的方法
    //3.设置线条的颜色
    //设置线条的颜色
    [[UIColor redColor] setStroke];
    //设置填充的颜色
    [[UIColor greenColor] setFill];

    //设置线条和填充的颜色一样
//    [[UIColor redColor] set];

    //4.绘制路径
    CGContextDrawPath(context, kCGPathFillStroke);
}

#pragma mark - 绘制线条1
- (void)drawLine:(CGContextRef)context {

    /*1.获取上下文(取得画布)*/
//    CGContextRef context = UIGraphicsGetCurrentContext();

    /*2.创建一个需要绘制的路径*/

    //    CGPathRef  ,不可变的
    //可变的,可以动态添加
    CGMutablePathRef path = CGPathCreateMutable();

    //划线
    //(1)设置起始点
    CGPathMoveToPoint(path, NULL, 50, 50);
    //(2)设置目标点
    CGPathAddLineToPoint(path, NULL, 200, 200);
    CGPathAddLineToPoint(path, NULL, 50, 200);

    //封闭路径
    CGPathCloseSubpath(path);

    /*3.将路径添加到上下文*/
    CGContextAddPath(context, path);

    /*4.设置上下文的属性*/

    //(1)设置线条的颜色
    /*
     设置线条的颜色
     red (0-1)    red/255.0
     green  (0-1)  green/255.0
     blue  (0-1)  blue/255.0
     */
    CGContextSetRGBStrokeColor(context, 35/255.0, 116/255.0, 0, 1.0);

    //(2)设置填充颜色
    CGContextSetRGBFillColor(context, 0, 0, 1, 1.0);

    //(3)设置线条的宽度
    CGContextSetLineWidth(context, 3);
    //设置两条线之间连接点的样式
    CGContextSetLineJoin(context, kCGLineJoinRound);
    //(4)设置圆角
    CGContextSetLineCap(context, kCGLineCapRound);
    //(5)设置虚线
    /*
     CGContextRef c           上下文
     CGFloat phase            相位,一般设置为0.f
     const CGFloat *lengths   C 数组
     size_t count             数组个数
     */
    CGFloat lengths[2] = {30,10};
    CGContextSetLineDash(context, 0.f, lengths, 2);

    /*5.绘制路径*/
    /*
     绘制模式
     kCGPathFill,  只填充,不会绘制边缘
     kCGPathStroke, 只绘制边缘,空心
     kCGPathFillStroke, 即画边缘,也填充
     */
    CGContextDrawPath(context, kCGPathFillStroke);

    /*6.释放路径*/
    CGPathRelease(path);

}

@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值