iOS Quartz 2D相关笔记总结笔记

一、基本理论和基本代码

  1. 概念总结

Quartz 2D是一个二维绘图引擎,同时支持iOS和Mac系统。Quartz 2D能完成的工作:
绘制图形 : 线条,三角形,矩形,圆,弧等;
绘制文字
绘制,生成图片(图像)
读取,生成PDF
截图,裁剪图片
自定义UI控件

Quartz2D提供了以下几种类型的Graphics Context:
Bitmap Graphics Context
PDF Graphics Context
Window Graphics Context
Layer Graphics Context
Printer Graphics Context

2.基本代码

1).获得图形上下文
CGContextRefctx= UIGraphicsGetCurrentContext();
2).拼接路径
CGContextMoveToPoint(ctx, 10, 10); CGContextAddLineToPoint(ctx, 100, 100);
3).绘制路径
CGContextStrokePath(ctx); CGContextFillPath(cox);

新建一个起点

voidCGContextMoveToPoint(CGContextRefc, CGFloatx, CGFloaty)

添加新的线段到某个点

voidCGContextAddLineToPoint(CGContextRefc, CGFloatx, CGFloaty)

添加一个矩形

CGContextAddRect(CGContextRefc, CGRectrect)

添加一个椭圆

voidCGContextAddEllipseInRect(CGContextRefcontext, CGRectrect)

添加一个圆弧

CGContextAddArc(CGContextRefc, CGFloatx, CGFloaty,CGFloatradius, CGFloatstartAngle, CGFloatendAngle, int clockwise)

Mode参数决定绘制的模式

CGContextDrawPath(CGContextRefc, CGPathDrawingModemode)

绘制空心路径

CGContextStrokePath(CGContextRefc)

绘制实心路径

CGContextFillPath(CGContextRefc)

提示:一般以CGContextDraw、CGContextStroke、CGContextFill开头的函数,都是用来绘制路径的

绘制一个进度条

- (void)drawRect:(CGRect)rect
{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGPoint centerP = CGPointMake(rect.size.width * 0.5, rect.size.height * 0.5);
    CGFloat radius = MIN(rect.size.width, rect.size.height) * 0.5 - 15;
    UIBezierPath* path2 = [UIBezierPath bezierPathWithArcCenter:centerP radius:radius startAngle:-M_PI_2 endAngle: M_PI_2 + M_PI*2 clockwise:YES];
    [[UIColor redColor] set];
    CGContextAddPath(ctx, path2.CGPath);

    CGContextDrawPath(ctx, kCGPathStroke);
    UIBezierPath* path3 = [UIBezierPath bezierPathWithArcCenter:centerP radius:radius-20 startAngle:-M_PI_2 endAngle: M_PI_2 + M_PI*2 clockwise:YES];
    [[UIColor redColor] set];
    CGContextAddPath(ctx, path3.CGPath);
    CGContextDrawPath(ctx, kCGPathStroke);
     UIBezierPath* path = [UIBezierPath bezierPathWithArcCenter:centerP radius:radius-10 startAngle:-M_PI_2 endAngle:M_PI * 2 * self.progress - M_PI_2 clockwise:YES];
    CGContextSetLineWidth(ctx, 20);
    [[UIColor blueColor] set];
    CGContextSetLineCap(ctx, kCGLineCapRound);
    CGContextAddPath(ctx, path.CGPath);
    CGContextDrawPath(ctx, kCGPathStroke);

}
效果图

二、常用举例

  1. 颜色转图像
- (UIImage*) createImageWithColor: (UIColor*) color
{
    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return theImage;
}

2.view截屏

-(UIImage*)captureView:(UIView*)view
{
    if (!view) {
        return nil;
    }
    UIGraphicsBeginImageContext(view.bounds.size);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage* image= UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
        //写入照片
        //    UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);

    return image;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 转载请带上出处, 谢谢. Quartz 2D是一个二维图形绘制引擎, 它支持iOS环境和Mac OS X非内核下的...
    Falme丶阅读 944评论 0 3
  • 生命的起落 一节翠绿的线段 一头开放 一头收敛 太阳做着年轻的舵手 四季里 奴着一艘黑白相携的桅船 行驶的 ...
    一池凹水凸龙阅读 445评论 3 4
  • 1. 昨天上班的路上,发生了这样一件事。 在公交车上,有一位老人突然上车,年纪不算太大,但也是属于老年人的年龄段了...
    首席人才官阅读 363评论 0 1
  • 踏进大学就相当于踏进了半个社会。 大学不像高中那样单纯,一心只读圣贤书。 大学不像社会那样复杂,两耳要闻所有事...
    北方的南方菇凉阅读 1,353评论 0 0