使用CoreGraphics绘图

由于近期在做一个手势密码相关的项目,绘制手势密码用到CoreGraphics框架。

兴趣驱动下,我查阅了一下iOS的绘图相关的框架,总结如下:
iOS的绘图框架有多种,我们平常最常用的就是UIKit,其底层是依赖CoreGraphics实现的,而且绝大多数的图形界面也都是由UIKit完成,并且UIImage、NSString、UIBezierPath、UIColor等都知道如何绘制自己,也提供了一些方法来满足我们常用的绘图需求。除了UIKit,还有CoreGraphics、Core Animation,Core Image,OpenGL ES等多种框架,来满足不同的绘图要求。各个框架的大概介绍如下:

UIKit:最常用的视图框架,封装度最高,都是OC对象
CoreGraphics:主要绘图系统,常用于绘制自定义视图,纯C的API,使用Quartz2D做引擎
CoreAnimation:提供强大的2D和3D动画效果
CoreImage:给图片提供各种滤镜处理,比如高斯模糊、锐化等
OpenGL-ES:主要用于游戏绘制,但它是一套编程规范,具体由设备制造商实现

CoreGraphics中绘图的使用分这几种:
1,UIGraphicsGetCurrentContext()函数开头的绘制.
2,UIGraphicsBeginImageContextWithOptions()函数开头的绘制,
3,CGPathCreateMutable().
4,还有UIBezierPath.

直观的几张图:


框架分析1.jpg
框架分析2.jpg
框架分析3.jpg

UI开头的这几个方法是对CoreGraphics方法的封装,底层是用CGPath的方法实现的,当然可能还加入了其它的框架。


这里做CoreGraphics的一些实际使用介绍

1.头像外加一层圆环

头像加外层圆环.jpg

代码如下:

    myImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    myImageView.backgroundColor = [UIColor lightGrayColor];
    UIImage *image = [UIImage imageNamed:@"circle_Test_0.jpeg"];
    //获取图片上下文
    UIGraphicsBeginImageContext(self.view.bounds.size);
    
    CGRect rect = CGRectMake(self.view.bounds.size.width / 2 - radius , self.view.bounds.size.height / 2 - radius, 2 * radius, 2 * radius);
    UIColor *boardColor = [UIColor yellowColor];
    CGFloat lineWidth = 5.0f;
    //绘图
    CGContextRef context = UIGraphicsGetCurrentContext();
    //绘制外面的圆圈
    CGContextAddEllipseInRect(context, rect);
    CGContextSetLineWidth(context, lineWidth);
    [boardColor setStroke];
    CGContextDrawPath(context, kCGPathEOFillStroke);
    //绘制图片
    CGRect imageRect = CGRectMake(rect.origin.x + lineWidth / 2 , rect.origin.y + lineWidth / 2 , rect.size.width - lineWidth , rect.size.height  - lineWidth);
    CGContextAddEllipseInRect(context, imageRect);
    CGContextClip(context);
    CGContextDrawPath(context, kCGPathEOFillStroke);
    
    //CGContextSaveGState/CGContextRestoreGState用于记录和恢复已存储的绘图context。这里可以不储存
//  1.  CGContextSaveGState(context);
    
    /***************************
     CoreGraphics和UIKit的坐标系不一致,所以需要下面两行代码进行转换  CoreGraphics和OPENGLES的坐标系y轴都是向下
     ***************************/
    
    //坐标系转换 y轴向上改为向下
    CGContextTranslateCTM(context, 0.0, 2 * radius);
    CGContextScaleCTM(context, 1, -1);
    
    CGRect re = CGRectMake(self.view.bounds.size.width / 2 - radius , - (self.view.bounds.size.height / 2 - radius), 2 * radius, 2 * radius);
    CGContextDrawImage(context, re, image.CGImage);
    
//  2.  CGContextRestoreGState(context);
    
    // 从图片上下文中获取绘制的图片
    UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
    // 关闭图片上下文
    UIGraphicsEndImageContext();
    [myImageView setImage:im];
    [self.view addSubview:myImageView];

2.利用Layer绘制加载动画

image

代码如下:

- (void)drawRect:(CGRect)rect
{
    CGFloat centerX = self.ZX_centerX;
    CGFloat centerY = self.ZX_centerY;
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    //绘制大的圆形
    CGContextSetFillColorWithColor(context, [UIColor lightGrayColor].CGColor);
    CGContextAddEllipseInRect(context, CGRectMake(centerX - radius, centerY - radius, 2 * radius, 2 * radius));
    CGContextDrawPath(context, kCGPathEOFill);
    //绘制内部圆形,颜色覆盖之前的圆
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextAddEllipseInRect(context, CGRectMake(centerX - insideRadius, centerY - insideRadius, 2 * insideRadius,2 * insideRadius));
    CGContextDrawPath(context, kCGPathEOFill);
    //添加一条线,作为起点
    CGContextMoveToPoint(context, centerX, centerY - insideRadius);
    CGContextAddLineToPoint(context, centerX, centerY - radius);
    CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
    CGContextSetLineWidth(context, .5);
    CGContextDrawPath(context, kCGPathStroke);
}

绘制圆环和一条线,为layer动画做准备。


-(CAShapeLayer *)zx_Layer
{
    if (!_zx_Layer)
    {
        _zx_Layer = [CAShapeLayer layer];
        
        //利用lineWidth绘制扇形
        _zx_Layer.lineWidth = ZXLineWidth;
        _zx_Layer.strokeColor = [UIColor grayColor].CGColor;
        _zx_Layer.fillColor = [UIColor clearColor].CGColor;
        
        CGMutablePathRef circlePath = CGPathCreateMutable();
        //绘制弧线
        CGPathAddArc(circlePath, NULL, self.ZX_centerX, self.ZX_centerY, radius - ZXLineWidth * .5, 1.5 * M_PI, 1.49999 * M_PI, NO);
        _zx_Layer.path = circlePath;
        CGPathRelease(circlePath);
        
        [self.layer addSublayer:_zx_Layer];
    }
    return _zx_Layer;
}

创建一个CAShapeLayer,绘制弧线


    CABasicAnimation *ani = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    ani.fromValue = @(_percent);
    ani.toValue = @(percent);
    //为了看起来的效果比较流畅 写死时间  second
    ani.duration = 20;
    ani.fillMode = kCAFillModeForwards;
    ani.removedOnCompletion = NO;
    ani.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    
    [self.zx_Layer addAnimation:ani forKey:nil];

创建基本动画CABasicAnimation,做动画。


总结:

 学习并使用了苹果CoreGraphics框架,路漫漫其修远兮,吾将上下而求索。不足之处,还望指正!

githubDemo

参考:

http://www.cocoachina.com/ios/20170809/20187.html

https://www.jianshu.com/p/bc5f799ea5fb

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容