Quartz 2D

// 1.获得图形上下文

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    

    // 2.拼接图形(路径)

    // 设置线段宽度

    CGContextSetLineWidth(ctx, 10);

    

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

    CGContextSetLineCap(ctx, kCGLineCapRound);

    

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

    CGContextSetLineJoin(ctx, kCGLineJoinRound);

    

    // 设置线段颜色

    CGContextSetRGBStrokeColor(ctx, 1, 0, 0, 1);

    // 设置一个起点

    CGContextMoveToPoint(ctx, 10, 10);

    // 添加一条线段到(100, 100)

    CGContextAddLineToPoint(ctx, 100, 100);

    

    // 渲染一次

    CGContextStrokePath(ctx);

// set : 同时设置为实心和空心颜色

    // setStroke : 设置空心颜色

    // setFill : 设置实心颜色

    [[UIColor whiteColor] set];

//    CGContextSetRGBFillColor(ctx, 0, 0, 1, 1);

 
 

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

    CGContextClosePath(ctx);

  // 1.取得图片

    UIImage *image = [UIImage imageNamed:@"me"];

    

    // 2.画(三种都可以,但是显示效果不同)

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

//    [image drawInRect:CGRectMake(0, 0, 150, 150)];

    [image drawAsPatternInRect:CGRectMake(0, 0, 200, 200)];

    

    // 3.画文字

    NSString *str = @"为xxx所画";

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

// 4.画文字

    NSString *str = @"哈哈哈哈Good morning hello hi hi hi hi";

    //    [str drawAtPoint:CGPointZero withAttributes:nil];

    

    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];

    // NSForegroundColorAttributeName : 文字颜色

    // NSFontAttributeName : 字体

    attrs[NSForegroundColorAttributeName] = [UIColor redColor];

    attrs[NSFontAttributeName] = [UIFont systemFontOfSize:50];

    [str drawInRect:cubeRect withAttributes:attrs];

原文地址:https://www.cnblogs.com/qingsongeasy/p/3662916.html