码迷,mamicode.com
首页 > 其他好文 > 详细

Quartz 2D 的一些使用

时间:2016-03-05 23:18:18      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:

Quartz 2D是一个绘图框架,最近看了一下它的官方文档以及提供的的demo。看着这些资料自己做了一些小结。

1、线段的绘制 (绘制一条线段)

    //获取图像上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    //设置线条颜色
    CGContextSetRGBStrokeColor(context, .5, .5, .5, 1);
    //设置线条宽度
    CGContextSetLineWidth(context, 1);
    //设置起点
    CGContextMoveToPoint(context, 10, 60);
    //设置终点
    CGContextAddLineToPoint(context, 300, 90);
    //闭合
    CGContextStrokePath(context);

  绘制多条线段

    CGPoint addLines[] = {
        CGPointMake(100, 100),
        CGPointMake(200, 100),
        CGPointMake(200, 200),
        CGPointMake(100, 200),
        CGPointMake(100, 100),
    };
    //绘制多个点的线段
    CGContextAddLines(context, addLines, sizeof(addLines)/sizeof(addLines[0]));

    CGContextSetLineJoin(context, kCGLineJoinRound);

    CGContextStrokePath(context);
    
    
    
    CGPoint stroleSegMents[] = {
        CGPointMake(100, 300),
        CGPointMake(200, 300),
        CGPointMake(200, 400),
        CGPointMake(100, 400),
        CGPointMake(300, 300),
    };
    
    CGContextStrokeLineSegments(context, stroleSegMents, sizeof(stroleSegMents)/sizeof(stroleSegMents[0]));

  线段的样式设置

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGFloat len[] = {10,10};
    CGContextSetAlpha(context, .5);
    //设置线段的样式  可以通过这种方式来绘制虚线
    CGContextSetLineDash(context, 10, len, sizeof(len)/sizeof(len[0]));
    CGContextAddRect(context, CGRectMake(100, 100, 200, 200));
    CGContextStrokePath(context);

 

2、矩形

  

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextAddRect(context, CGRectMake(100, 100, 100, 100));
    CGContextFillPath(context);
    //CGContextStrokePath(context);
    
    CGContextStrokeRect(context, CGRectMake(250, 100, 100, 100));
    
    CGRect rec[] = {
        CGRectMake(100, 220, 100, 100),
        CGRectMake(100, 320, 100, 100),
        CGRectMake(100, 420, 100, 100),

    };
    CGContextAddRects(context, rec, sizeof(rec)/sizeof(rec[0]));
    CGContextStrokePath(context);




    CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
    CGContextSetLineWidth(context, 2.0);
    CGPoint center;
    center = CGPointMake(90.0, 150.0);
    CGContextMoveToPoint(context, center.x, center.y + 60.0);
    for(int i = 1; i < 3; ++i)
    {
        CGFloat x = 60.0 * sinf(i * 4.0 * M_PI / 5.0);
        CGFloat y = 60.0 * cosf(i * 4.0 * M_PI / 5.0);
        CGContextAddLineToPoint(context, center.x + x, center.y + y);
    }
    CGContextClosePath(context);
    center = CGPointMake(210.0, 310.0);
    CGContextMoveToPoint(context, center.x, center.y + 60.0);
    for(int i = 1; i < 6; ++i)
    {
        CGFloat x = 60.0 * sinf(i * 2.0 * M_PI / 6.0);
        CGFloat y = 60.0 * cosf(i * 2.0 * M_PI / 6.0);
        CGContextAddLineToPoint(context, center.x + x, center.y + y);
    }
    CGContextClosePath(context);
    CGContextDrawPath(context, kCGPathEOFill);

 

 

3、弧度(圆)

    CGContextRef context = UIGraphicsGetCurrentContext();
    //通过矩形范围绘制
    CGContextAddEllipseInRect(context, CGRectMake(60.0, 100.0, 60.0, 60.0));
    CGContextStrokePath(context);

    CGContextStrokeEllipseInRect(context, CGRectMake(30.0, 180.0, 60.0, 60.0));

    //填充绘制
    CGContextFillEllipseInRect(context, CGRectMake(30.0, 290.0, 60.0, 60.0));

    /**
     *  参数从左之右
        图形上下文   x,y 弧度 起点弧度,终点弧度 ,是否顺时针
     */
    CGContextAddArc(context, 180, 160, 100.0, -M_PI/2, M_PI, false);
    CGContextStrokePath(context);
    
    
    CGContextAddArc(context, 170.0, 160.0, 60.0, M_PI/4.0, M_PI, true);
    CGContextStrokePath(context);
    
    CGContextAddArc(context, 150.0, 150.0, 30.0, 0.0, M_PI/2.0, false);
    CGContextAddArc(context, 150.0, 150.0, 30.0, 3.0*M_PI/2.0, M_PI, true);
    CGContextStrokePath(context);

    
    CGContextAddArc(context, 150.0, 340.0, 30.0, 0.0, M_PI/2.0, false);
    CGContextAddArc(context, 150.0, 340.0, 30.0, M_PI, 3.0*M_PI/2.0, false);
    CGContextStrokePath(context);

    
    CGPoint p[3] =
    {
        CGPointMake(310.0, 30.0),
        CGPointMake(310.0, 60.0),
        CGPointMake(340.0, 60.0),
    };
    CGContextMoveToPoint(context, p[0].x, p[0].y);
    CGContextAddArcToPoint(context, p[1].x, p[1].y, p[2].x, p[2].y, 30.0);
    CGContextStrokePath(context);

    CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
    CGContextAddLines(context, p, sizeof(p)/sizeof(p[0]));
    CGContextStrokePath(context);

    
    CGContextSetRGBStrokeColor(context, .5, 1.0, 1.0, 1.0);
    CGContextSetLineWidth(context, 2.0);
    
    CGPoint s = CGPointMake(30.0, 320.0);
    CGPoint e = CGPointMake(300.0, 320.0);
    CGPoint cp1 = CGPointMake(320.0, 30.0);
    CGPoint cp2 = CGPointMake(210.0, 210.0);
    CGContextMoveToPoint(context, s.x, s.y);
    CGContextAddCurveToPoint(context, cp1.x, cp1.y, cp2.x, cp2.y, e.x, e.y);
    CGContextStrokePath(context);
    
    CGContextSetRGBStrokeColor(context, 1.0, 0.5, 0.0, 1.0);
    CGContextMoveToPoint(context, s.x, s.y);
    CGContextAddLineToPoint(context, cp1.x, cp1.y);
    CGContextMoveToPoint(context, e.x, e.y);
    CGContextAddLineToPoint(context, cp2.x, cp2.y);
    CGContextStrokePath(context);

    CGContextSetRGBStrokeColor(context, .5, 1.0, 1.0, 1.0);
    s = CGPointMake(30.0, 300.0);
    e = CGPointMake(270.0, 300.0);
    cp1 = CGPointMake(150.0, 180.0);
    CGContextMoveToPoint(context, s.x, s.y);
    CGContextAddQuadCurveToPoint(context, cp1.x, cp1.y, e.x, e.y);
    CGContextStrokePath(context);
    
    CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
    CGContextMoveToPoint(context, s.x, s.y);
    CGContextAddLineToPoint(context, cp1.x, cp1.y);
    CGContextStrokePath(context);

    

4 、通过图片绘制

    CGContextRef context = UIGraphicsGetCurrentContext();
    //翻转坐标系
    CGContextTranslateCTM(context, 0.0, self.frame.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGImageRef img = [UIImage imageNamed:@"1024x1024"].CGImage;
    CGContextDrawImage(context, CGRectMake(30, 80, 60, 60), img);
    CGContextDrawTiledImage(context, CGRectMake(30, 80, 60, 60), img);
    CGContextStrokePath(context);

5、通过pdf文件绘制

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, 0.0, self.frame.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    
    CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(),CFSTR("input_pdf.pdf"),NULL,NULL);
    CGPDFDocumentRef document = CGPDFDocumentCreateWithURL(pdfURL);
    CFRelease(pdfURL);
    
    CGPDFPageRef page = CGPDFDocumentGetPage(document, size_page);
    CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, self.bounds, 0, true);
    CGContextConcatCTM(context, pdfTransform);

    CGContextDrawPDFPage(context, page);

 

Quartz 2D 的一些使用

标签:

原文地址:http://www.cnblogs.com/lsios/p/5245994.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!