iOS Quartz 2D相关笔记总结笔记 韩俊强的博客
2016-12-01 22:13
323 查看
一、基本理论和基本代码
概念总结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
相关文章推荐
- IOS学习笔记 运用Quartz 2D框架绘制常见的图形(2)
- IOS Quartz2D 相关
- iOS小明开发笔记(十五) (Quartz2D矩阵操作)
- iOS学习笔记08-Quartz2D绘图
- iOS开发中的零碎知识点笔记 韩俊强的博客
- iOS-Quartz2D 之 画图预览笔记
- iOS Quartz 2D绘图用CGContextRef绘制三角形 —— HERO博客
- iOS小明开发笔记(十六) (Quartz2D图片剪切)
- iOS小明开发笔记(十二) (Quartz2D简单使用一)
- iOS学习笔记08-Quartz2D绘图
- iOS笔记052- Quartz2D-绘图
- iOS中崩溃调试的使用和技巧总结 韩俊强的博客
- 【IOS 开发学习总结-OC-64】Quartz 2D绘图(4-1)——Quartz 2D绘图基础+点线模式
- iOS小明开发笔记(十七) (Quartz2D屏幕截图)
- iOS Quartz 2D 学习总结
- ios学习笔记:关于Quartz2D使用(矩阵操作)
- 【IOS 开发学习总结-OC-66】Quartz 2D绘图(4-3)——绘制曲线+在内存中绘图+绘制位图
- 【IOS 开发学习总结-OC-67】Quartz 2D绘图(4-4)——图形变换+填充处理+core image 滤镜
- iOS海哥开发笔记 (海哥原创,Quartz-2d的简单使用 一)
- iOS开发中的零碎知识点笔记 韩俊强的博客