IOS 绘图

1.绘制线条

override func drawRect(rect: CGRect) {

        // Drawing code

        var context = UIGraphicsGetCurrentContext()

        CGContextMoveToPoint(context, 100.0, 100.0)

        CGContextAddLineToPoint(context, 100, 200)

        CGContextSetLineWidth(context, 5.0)     //set line width

        CGContextSetRGBStrokeColor(context, 1, 0, 0, 1.0) //set line color

        CGContextStrokePath(context)

    }

 override func drawRect(rect: CGRect) {

        // Drawing code

        var context = UIGraphicsGetCurrentContext()

        var path = CGPathCreateMutable()

        CGPathMoveToPoint(path, nil, 100, 100)

        CGPathAddLineToPoint(path, nil, 100, 200)

        CGContextAddPath(context, path)

        CGContextStrokePath(context)

       

    }

2.绘制矩形

override func drawRect(rect: CGRect) {

        // Drawing code

        var context = UIGraphicsGetCurrentContext()

        CGContextAddRect(context, CGRect(x: 100, y: 100, 200, height: 200))

        CGContextSetRGBFillColor(context, 1, 0, 0, 1.0) //set fill color

        CGContextFillPath(context)

        

        //添加边框

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

        CGContextSetLineWidth(context, 5)

        CGContextStrokeRect(context, CGRect(x: 100, y: 100, 200, height: 200))

    }

3.绘制圆弧

  override func drawRect(rect: CGRect) {

        // Drawing code

        var context = UIGraphicsGetCurrentContext()

        //顺时针 180度圆弧

        CGContextAddArc(context, 150, 300, 100, 0, 3.14, 0);

    

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

        CGContextSetLineWidth(context, 5)

        CGContextStrokePath(context)

    }

 4.填充圆

override func drawRect(rect: CGRect) {

        // Drawing code

        var context = UIGraphicsGetCurrentContext()

        //填充圆

        CGContextAddArc(context, 150, 300, 100, 0, 3.14*2, 0)

        CGContextFillPath(context)

    }

override func drawRect(rect: CGRect) {

        // Drawing code

        var context = UIGraphicsGetCurrentContext()

        //填充圆

        CGContextAddEllipseInRect(context, CGRect(x: 200, y: 200, 200, height: 200))

//        CGContextFillPath(context)

        

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

        CGContextSetLineWidth(context, 5)

        CGContextStrokePath(context)

        

    }

5.绘制图片

required init?(coder aDecoder: NSCoder) {

        super.init(coder: aDecoder)

        uiImage =  UIImage(named: "walle.png")!.CGImage

        

    }

    // Only override drawRect: if you perform custom drawing.

    // An empty implementation adversely affects performance during animation.

    override func drawRect(rect: CGRect) {

        // Drawing code

        var context = UIGraphicsGetCurrentContext()

        CGContextSaveGState(context)

        CGContextTranslateCTM(context,0, 220)

        CGContextScaleCTM(context, 1, -1)

        CGContextDrawImage(context, CGRect(x: 0, y: 0, 300, height: 200), uiImage)

        CGContextRestoreGState(context)

    }

    var uiImage:CGImageRef?

 6.绘制进度圆饼

var ctx = UIGraphicsGetCurrentContext()

        

          var r = rect.width/2

        

        CGContextAddArc(ctx, r,r ,r, 0, 3.141592653*2*r, 0)

        

        CGContextAddLineToPoint(ctx, r, r)

        

        CGContextSetRGBFillColor(ctx, 0.7, 0.7, 0.7, 1)

        

        CGContextFillPath(ctx)

        

      

        

        CGContextAddArc(ctx, r,r ,r, 0, 3.141592653*2*_progressValue, 0)

        

        CGContextAddLineToPoint(ctx, r, r)

        

        CGContextSetRGBFillColor(ctx, 0, 0, 1, 1)

        

        CGContextFillPath(ctx)

        

7.矩阵操作

//1.获取图形上下文

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    

    //2.描述路径

    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-50, -50, 200, 100)];

    

    [[UIColor redColor]set];

    

    //平移

    CGContextTranslateCTM(ctx, 100, 50);

    

    //缩放

    CGContextScaleCTM(ctx, 0.5, 0.5);

    

    //旋转

    CGContextRotateCTM(ctx, M_PI_4);

    

    //3.添加路径

    CGContextAddPath(ctx, path.CGPath);

    

    //4.渲染上下文

    CGContextFillPath(ctx);

8.图片水印

  //加载图片

    UIImage *image = [UIImage imageNamed:@"123.png"];

    

    //开启位图上下文

    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);

//    //获取位图上下文

//    CGContextRef ctx = UIGraphicsGetCurrentContext();

    //描述路径

    

    //渲染上下文

    

    //1.绘制原生图片

    [image drawAtPoint:CGPointZero];

    

    //2.给原生图片添加文字

    NSString *str = @"乐乐专用";

    NSMutableDictionary *dicM = [NSMutableDictionary dictionary];

    

    dicM[NSFontAttributeName] = [UIFont systemFontOfSize:38];

    dicM[NSForegroundColorAttributeName] = [UIColor blueColor];

    [str drawAtPoint:CGPointMake(150, 50) withAttributes:dicM];

    

    //3.生成一张图片,我们从上下文获取

    

    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();

    _imageView.image = img;

    

    UIGraphicsEndImageContext();

       

原文地址:https://www.cnblogs.com/PJXWang/p/4915478.html