参考 :http://www.mgenware.com/blog/?p=493

这三种东西:CGContextRefCGPathUIBezierPath。本质上都是一样的,都是使用Quartz来绘画。只不过把绘图操作暴露在不同的API层面上,在具体实现上,当然也会有一些细小的差别。

我们将主要使用这3个类型,绘制出同一张图片,如下,一个笑脸:

首先使用Quartz的CGPath来做这张图。很简单,首先创建用于转移坐标的Transform,然后创建一个CGMutablePathRef(属于CGPath类型)对象。接着通过两个CGPathAddEllipseInRect和一个CGPathAddArc函数来绘制Path中的两个眼睛和一个嘴,注意把CGAffineTransform的地址传进去,这样Transform才会应用。接着把这个创建好的CGPath加入到当前CGContextRef中,最后通过CGContextRef执行绘画。

代码:

- (void)viewDidLoad
{
[super viewDidLoad]; //开始图像绘图
UIGraphicsBeginImageContext(self.view.bounds.size);
//获取当前CGContextRef
CGContextRef gc = UIGraphicsGetCurrentContext(); //创建用于转移坐标的Transform,这样我们不用按照实际显示做坐标计算
CGAffineTransform transform = CGAffineTransformMakeTranslation(, );
//创建CGMutablePathRef
CGMutablePathRef path = CGPathCreateMutable();
//左眼
CGPathAddEllipseInRect(path, &transform, CGRectMake(, , , ));
//右眼
CGPathAddEllipseInRect(path, &transform, CGRectMake(, , , ));
//笑
CGPathMoveToPoint(path, &transform, , );
CGPathAddArc(path, &transform, , , , , M_PI, NO);
//将CGMutablePathRef添加到当前Context内
CGContextAddPath(gc, path);
//设置绘图属性
[[UIColor blueColor] setStroke];
CGContextSetLineWidth(gc, );
//执行绘画
CGContextStrokePath(gc); //从Context中获取图像,并显示在界面上
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
[self.view addSubview:imgView];
}

接下来,我们不去使用CGPath类型的相关函数,而完全使用CGContextRef相关的函数,这些函数执行起来其实是和上面讲的的CGPath完全等价的。

这里需要注意的是,完全使用CGContextRef的话,Transform的应用需使用CGContextTranslateCTM函数。

完整代码:

- (void)viewDidLoad
{
[super viewDidLoad]; //开始图像绘图
UIGraphicsBeginImageContext(self.view.bounds.size);
//获取当前CGContextRef
CGContextRef gc = UIGraphicsGetCurrentContext(); //使用CGContextTranslateCTM函数来转移坐标的Transform,这样我们不用按照实际显示做坐标计算
CGContextTranslateCTM(gc, , );
//左眼
CGContextAddEllipseInRect(gc, CGRectMake(, , , ));
//右眼
CGContextAddEllipseInRect(gc, CGRectMake(, , , ));
//笑
CGContextMoveToPoint(gc, , );
CGContextAddArc(gc, , , , , M_PI, NO);
//设置绘图属性
[[UIColor blueColor] setStroke];
CGContextSetLineWidth(gc, );
//执行绘画
CGContextStrokePath(gc); //从Context中获取图像,并显示在界面上
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
[self.view addSubview:imgView];
}

同样会绘制出上面的图形。

最后我们使用UIBezierPath类型来完成上述图形,UIBezierPath很有意思,它包装了Quartz的相关API,自己存在于UIKit中,因此不是基于C的API,而是基于Objective-C对象的。那么一个非常重要的点是由于离开了Quartz绘图,所以不需要考虑Y轴翻转的问题,在画弧的时候,clockwise参数是和现实一样的,如果需要顺时针就传YES,而不是像Quartz环境下传NO的。

其次椭圆的创建需使用bezierPathWithOvalInRect方法,这里名字是Oral而不是Quartz中的Ellipse

最后注意UIBezierPathapplyTransform方法需要最后调用。

完整代码:

- (void)viewDidLoad
{
[super viewDidLoad]; //开始图像绘图
UIGraphicsBeginImageContext(self.view.bounds.size); //创建UIBezierPath
UIBezierPath *path = [UIBezierPath bezierPath];
//左眼
[path appendPath:[UIBezierPath bezierPathWithOvalInRect:CGRectMake(, , , )]];
//右眼
[path appendPath:[UIBezierPath bezierPathWithOvalInRect:CGRectMake(, , , )]];
//笑
[path moveToPoint:CGPointMake(, )];
//注意这里clockwise参数是YES而不是NO,因为这里不知Quartz,不需要考虑Y轴翻转的问题
[path addArcWithCenter:CGPointMake(, ) radius: startAngle: endAngle:M_PI clockwise:YES];
//使用applyTransform函数来转移坐标的Transform,这样我们不用按照实际显示做坐标计算
[path applyTransform:CGAffineTransformMakeTranslation(, )];
//设置绘画属性
[[UIColor blueColor] setStroke];
[path setLineWidth:];
//执行绘画
[path stroke]; //从Context中获取图像,并显示在界面上
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
[self.view addSubview:imgView];
}

[ios]ios画线 使用CGContextRef,CGPath和UIBezierPath来绘画的更多相关文章

  1. iOS: 使用CGContextRef,CGPath和UIBezierPath来绘画

    这三种东西:CGContextRef,CGPath和UIBezierPath.本质上都是一样的,都是使用Quartz来绘画.只不过把绘图操作暴露在不同的API层面上,在具体实现上,当然也会有一些细小的 ...

  2. ios cocos2d 画线出现闪烁问题

    根据http://www.merowing.info/2012/04/drawing-smooth-lines-with-cocos2d-ios-inspired-by-paper/ 用cocos2d ...

  3. [修复] Firemonkey 画线问题(Android & iOS 平台)

    问题:官方 QC 的一个 Firemonkey 移动平台画线问题: RSP-14309: [iOS & Android] Delphi 10.1 Berlin - drawing proble ...

  4. IOS Quartz 各种绘制图形用法---实现画图片、写文字、画线、椭圆、矩形、棱形等

    // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affec ...

  5. IOS 绘制基本图形( 画圆、画线、画圆弧、绘制三角形、绘制四边形)

    // 当自定义view第一次显示出来的时候就会调用drawRect方法- (void)drawRect:(CGRect)rect { // 1.获取上下文 CGContextRef ctx = UIG ...

  6. iOS小画板画线总结

    一:基本画线: 使用贝赛尔曲线画: //创建路径 UIBezierPath* aPath = [UIBezierPath bezierPath]; //设置线宽 aPath.lineWidth = 5 ...

  7. [ios]MKMapView中使用MKPolyline画线

    参考:http://blog.sina.com.cn/s/blog_9e8867eb0101dt76.html 首先在MapView.h中 #import <MapKit/MapKit.h> ...

  8. CGContextRef 画线简单用法

    CGContextRef CGContextMoveToPoint(context,150,50);//圆弧的起始点 CGContextAddArcToPoint(context,100,80,130 ...

  9. IOS 股票K线图、分时图

    IOS 股票K线图.分时图,网上开源项目很少,质量也是参差不齐:偶尔搜索到看似有希望的文章,点进去,还是个标题党:深受毒害.经过一段时间的探索,终于在开源基础上完成了自己的股票K线图.分时图: 先放出 ...

随机推荐

  1. 使用HttpClient进行远程接口测试

    前两天在工作中,项目组长给我了一个远程接口让我给测一下,因为是http协议,所以我首先想到了用httpClient工具类来测试,网上一查,找到了好多示例代码,随便复制了一个demo进行了简单的修改,结 ...

  2. Log4net 日志传到 graylog监控

    graylog是java的一个日志监控插件.存储用的是mongoDB,效率还是挺高的.不过嘛,文档太少了,安装和配置都很不容易. 官网:http://www.graylog.org/ 在graylog ...

  3. Lintcode: Lowest Common Ancestor

    Given the root and two nodes in a Binary Tree. Find the lowest common ancestor(LCA) of the two nodes ...

  4. C#中NPOI操作excel之读取和写入excel数据

    一.下载引用 下载需要引用的dll,即:NPOI.dll,NPOI.OOXML.dll,NPOI.OpenXml4Net.dll,ICSharpCode.SharpZipLib.dll(office2 ...

  5. sql server2012重复执行创建表视图sql及带行号的视图

    1.可重复操作(创建表,视图)的sql语句判断 IF EXISTS ( SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[re ...

  6. apache源码安装

    1.apr和apr-util,下载地址: http://apr.apache.org/download.cgi yum install gcc yum install libtool yum inst ...

  7. Python: 字典列表: 通过某个字段将记录分组

    问题:有一个字典或者实例的序列,想根据某个特定的字段比如date 来分组迭代访问. answer: itertools.groupby函数对于这样的数据分组操作非常实用 eg: rows = [{'a ...

  8. thinkphp标签实现bootsrtap轮播carousel实例

    thinkphp标签实现bootsrtap轮播carousel实例由于轮播carousel第一个div需要设置active样式才能正常显示,上面的圆点也同样需要数字,使用volist标签在循环的同时可 ...

  9. Docker容器和数据可视化管理工具Flocker

    Flocker 可轻松实现 Docker 容器及其数据的管理.这是一个数据卷管理器和多主机的 Docker 集群管理工具,你可以通过它来控制数据.可用来在 Docker 中运行你的数据库.查询和 K/ ...

  10. python模块-json、pickle、shelve

    json模块 用于文件处理时其他数据类型与js字符串之间转换.在将其他数据类型转换为js字符串时(dump方法),首先将前者内部所有的单引号变为双引号,再整体加上引号(单或双)转换为js字符串:再使用 ...