Quartz2D绘图的基本使用

#pragma mark 绘制文本

- (void)drawText

{

    NSString *text = @"床上明月光,疑是地上霜.";

    UIFont *font = [UIFontsystemFontOfSize:17];

    NSDictionary *dict = @{

                           NSFontAttributeName : font,

                           NSForegroundColorAttributeName : [UIColorredColor]

                           };

    CGRect rect = [text boundingRectWithSize:CGSizeMake(20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOriginattributes:dict context:nil];

    //设置rectorgin可以改变起始位置

    rect.origin = CGPointMake(100, 20);

    [text drawInRect:rect withAttributes:dict];

}

#pragma mark 画图像

- (void)drawImage

{

    UIImage *image = [UIImageimageNamed:@"头像1"];

//    //1.在指定坐标绘制

//    [image drawAtPoint:CGPointMake(80, 80)];

//    //2.在指定区域拉伸绘制

//    [image drawInRect:self.bounds];

    //3.平铺绘制

    [image drawAsPatternInRect:self.bounds];

}

#pragma mark 画圆弧

- (void)drawArc

{

    //1.获取上下文

    CGContextRef context = UIGraphicsGetCurrentContext();

    //2.添加圆弧到上下文

    CGContextAddArc(context, 160, 160, 100, -M_PI_2, 0, 1);

    //3.绘制

    CGContextDrawPath(context, kCGPathStroke);

}

#pragma mark 利用上下文画圆

- (void)drawEllipse

{

    //1.获取上下文

    CGContextRef context = UIGraphicsGetCurrentContext();

    //2.指定绘制圆形外切的矩形

    CGRect rect = CGRectMake(100, 100, 150, 150);

    //3.添加圆形到上下文路径

    CGContextAddEllipseInRect(context, rect);

    [[UIColorredColor] setStroke];

    //4.开始绘制

    CGContextDrawPath(context, kCGPathStroke);

}

#pragma mark 绘制矩形

- (void)drawRectangle

{

    //1.指定一个CGRect

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

    [[UIColorredColor] set];

    //2.绘制实心的矩形

    UIRectFill(rect);

    //3.绘制一个空心的矩形

   CGRect rect1 = CGRectMake(150, 400, 50, 50);

    [[UIColorblueColor] set];

    UIRectFrame(rect1);

}

#pragma mark 利用上下文画一个三角形

- (void)drawTriangle

{

    //1.获取上下文

    CGContextRef context = UIGraphicsGetCurrentContext();

    //2.设置当前点

    CGContextMoveToPoint(context, 100, 100);

    //3.增加一条直线

    CGContextAddLineToPoint(context, 200, 200);

    //3.1在增加一条直线

    CGContextAddLineToPoint(context, 200, 100);

    //4.设置颜色

    [[UIColorredColor] setFill];

    //5.关闭路径

    CGContextClosePath(context);

    //6.开始绘制

    CGContextDrawPath(context, kCGPathFill);

}

#pragma mark  利用上下文绘制一条直线

- (void)drawLineWithContext

{

    //1.获取上下文

    CGContextRef context = UIGraphicsGetCurrentContext();

    //2.设置当前点

    CGContextMoveToPoint(context, 100, 100);

    //3.增加一条直线

    CGContextAddLineToPoint(context, 200, 200);

    //4.设置颜色

    CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);

    //5.开始绘制

    CGContextDrawPath(context, kCGPathStroke);

    

}

#pragma mark 绘制一条直线

- (void)drawLine

{

    //1.获取当前图形的上下文,就是要绘制到屏幕上的

    CGContextRef context = UIGraphicsGetCurrentContext();

    //2.创建路径

    CGMutablePathRef path = CGPathCreateMutable();

        //2.1设置当前点

    CGPathMoveToPoint(path, nil, 100, 100);

        //2.2增加一条直线

    CGPathAddLineToPoint(path, nil, 200, 200);

    //3.将路径添加到上下文

    CGContextAddPath(context, path);

    //4.设置上下文

    CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);

    //5.绘制

    CGContextDrawPath(context, kCGPathStroke);

    //6.释放路径

    CGPathRelease(path);

}

原文地址:https://www.cnblogs.com/yinqiang/p/3483023.html