iOS Quartz 2D相关笔记总结笔记 韩俊强的博客
来源:互联网 发布:英雄联盟网吧特权软件 编辑:程序博客网 时间:2024/06/05 15:37
一、基本理论和基本代码
- 概念总结
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);}
二、常用举例
- 颜色转图像
- (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;}
每日更新关注:http://weibo.com/hanjunqiang 新浪微博!iOS开发者交流QQ群: 446310206
11 0
- iOS Quartz 2D相关笔记总结笔记 韩俊强的博客
- Quartz 2D学习笔记
- Quartz 2D学习笔记
- IOS学习笔记 运用Quartz 2D框架绘制常见的图形(2)
- iOS海哥开发笔记 (海哥原创,Quartz-2d的简单使用 一)
- iOS Quartz 2D 学习总结
- 学习笔记 - Quartz 2D绘图的基本步骤
- Quartz 2D Programming Guide--笔记
- Quartz 2D学习笔记--基础篇
- Quartz 2D学习笔记--Image篇
- 学习笔记 - Quartz 2D绘图 -
- Quartz 2D 相关的知识
- iOS海哥开发笔记 (海哥原创,绘图Quartz-2d的简单使用 二)
- IOS Quartz 2D
- iOS-Quartz 2D
- IOS Quartz 2D
- ios-Quartz 2D
- 博客笔记总结2
- gradient descend/accelerated gradient descend/linearSVM/libSVM
- Dubbo cvc-complex-type.2.4.c 错误,dubbo.xsd
- VMware尝试访问日志文件时出错
- C语言输出指针变量地址以及指向变量的地址
- PHP计算工作日的问题
- iOS Quartz 2D相关笔记总结笔记 韩俊强的博客
- [LeetCode]Decode Ways
- Dynamics CRM2016 Web Api之分页查询
- <c:forEach> 标签的var 属性和varStatus属性的区别
- HDU 1008 Elevator 水题
- 【一点一点写算法】排序算法开头
- 学习笔记之--JAVA IO流
- Jsp 使用 fileupload 文件上传和下载
- BZOJ4720——[Noip2016]换教室