1、什么是Quartz2D?二维的绘图引擎
Quartz 2D是一个二维绘图引擎,同时支持iOS和Mac系统,用Quartz2D写的同一份代码,既可以运行在iphone上又可以运行在mac上,可以跨平台开发。 开发中比较常用的是截屏/裁剪/自定义UI控件。 Quartz2D在iOS开发中的价值就是自定义UI控件。
2、Quartz 2D能完成的工作
绘制图形 : 线条\三角形\矩形\圆\弧等
绘制文字
绘制\生成图片(图像)
读取\生成PDF
截图\裁剪图片
自定义UI控件… …
3、图形上下文的数据类型
图形上下文(Graphics Context):是一个CGContextRef类型的数据
4、图形上下文的作用
保存绘图信息、绘图状态
决定绘制的输出目标(绘制到什么地方去?)
(输出目标可以是PDF文件、Bitmap或者显示器的窗口上)
5、有多少种上下文。
Bitmap Graphics Context
PDF Graphics Context
Window Graphics Context
Layer Graphics Context
Printer Graphics Context
6、只有在drawRect:方法中才能获取到上下文,当你视图第一次显示的时候就会调用;当你要手动刷新屏幕的时候,执行[self setNeedsDisplay]
就会调用drawRect.
7、Quartz2D绘图演练
绘制线段
123456789101112131415161718192021 // 1.获取上下文// CGContextRef CG CoreGraphics Ref 引用// 目前学的上下文都跟UIGraphics有关,以后想直接获取上下文,直接敲一个UIGraphicsCGContextRef ctx = UIGraphicsGetCurrentContext();// 2.设置绘图信息(拼接路径)UIBezierPath *path = [UIBezierPath bezierPath];// 设置起点[path moveToPoint:CGPointMake(10, 10)];// 添加一条线到某个点[path addLineToPoint:CGPointMake(125, 125)];[path addLineToPoint:CGPointMake(240, 10)];// 3.把路径添加到上下文// 直接把UIKit的路径转换成CoreGraphics,CG开头就能转CGContextAddPath(ctx, path.CGPath);// 4.把上下文渲染到视图// Stroke描边CGContextStrokePath(ctx);画文字
123456789101112 NSString *text = @"hello motolosdmotolosdmotolosdmotolosdmotolosdmotolosdmotolosdmotolosdmotolosdmotolosdmotolosdmotolosdmotolosd";CGRect textFrame = CGRectMake(0, 0, 250, 250);NSDictionary *dict = @{NSFontAttributeName : [UIFont systemFontOfSize:20],NSForegroundColorAttributeName : [UIColor redColor],NSStrokeWidthAttributeName : @5};//UIRectFill(textFrame);//[text drawInRect:textFrame withAttributes:dict];/*text drawInRect:textFrame withAttributes:dict]; 会自动换行[text drawAtPoint:CGPointZero withAttributes:dict]; 不会自动换行*/[text drawAtPoint:CGPointZero withAttributes:dict];画图片
123456789101112 // Drawing codeUIImage *image = [UIImage imageNamed:@"头像"];//[image drawAtPoint:CGPointZero];//[image drawInRect:CGRectMake(0, 0, 100, 100)];// 设置裁剪区域,超出裁剪区域的都会被裁剪掉UIRectClip(CGRectMake(0, 0, 100, 100));UIImage *pImage = [UIImage imageNamed:@"001"];[pImage drawAsPatternInRect:rect];画曲线 3个点,起点,终点,控制点。
1234567891011121314151617 // 1.获取上下文CGContextRef ctx = UIGraphicsGetCurrentContext();// 2.拼接路径UIBezierPath *path = [UIBezierPath bezierPath];CGPoint startP = CGPointMake(10, 125);CGPoint endP = CGPointMake(240, 125);CGPoint controlP = CGPointMake(125, 0);[path moveToPoint:startP];[path addQuadCurveToPoint:endP controlPoint:controlP];// 3.把路径添加到上下文CGContextAddPath(ctx, path.CGPath);// 4.渲染上下文到视图CGContextStrokePath(ctx);
8、CADisplayLink
12345 // iphone每秒刷新60次// 屏幕刷新的时候就会触发CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(setNeedsDisplay)];[link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
9、图形上下文的栈
123456789101112131415161718192021222324252627282930313233343536373839 // 1.获取上下文CGContextRef ctx = UIGraphicsGetCurrentContext();// 把ctx拷贝一份放在栈中CGContextSaveGState(ctx);// 2.拼接路径(绘图的信息)UIBezierPath *path = [UIBezierPath bezierPath];[path moveToPoint:CGPointMake(10, 125)];[path addLineToPoint:CGPointMake(240, 125)];// 3.路径添加到上下文CGContextAddPath(ctx, path.CGPath);// 设置绘图的状态[[UIColor redColor] set];CGContextSetLineWidth(ctx, 10);CGContextSetLineCap(ctx, kCGLineCapRound);// 4.渲染CGContextStrokePath(ctx);// 第二根线UIBezierPath *path1 = [UIBezierPath bezierPath];[path1 moveToPoint:CGPointMake(125, 10)];[path1 addLineToPoint:CGPointMake(125, 240)];CGContextAddPath(ctx, path1.CGPath);// 把栈顶上下文取出来,替换当前上下文CGContextRestoreGState(ctx);// 设置绘图的状态//[[UIColor blackColor] set];//CGContextSetLineWidth(ctx, 1);//CGContextSetLineCap(ctx, kCGLineCapButt);// 4.渲染CGContextStrokePath(ctx);
10、图形上下文的矩阵操作
1234567891011121314151617181920212223 // 1.获取上下文CGContextRef ctx = UIGraphicsGetCurrentContext();// 注意:你的路径一定放在上下文矩阵操作之后// 平移上下文CGContextTranslateCTM(ctx, 50, 100);// 旋转上下文CGContextRotateCTM(ctx, M_PI_4);// 缩放上下文CGContextScaleCTM(ctx, 0.5, 1.2);// 2.拼接路径UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-50, -100, 150, 200)];// 3.把路径添加到上下文CGContextAddPath(ctx, path.CGPath);[[UIColor yellowColor] set];// 4.渲染CGContextFillPath(ctx);
11、图片裁剪
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 + (instancetype)imageWithName:(NSString *)name border:(CGFloat)border borderColor:(UIColor *)color{// 圆环的宽度CGFloat borderW = border;// 加载旧的图片UIImage *oldImage = [UIImage imageNamed:name];// 新的图片尺寸CGFloat imageW = oldImage.size.width + 2 * borderW;CGFloat imageH = oldImage.size.height + 2 * borderW;// 设置新的图片尺寸CGFloat circirW = imageW > imageH ? imageH : imageW;// 开启上下文UIGraphicsBeginImageContextWithOptions(CGSizeMake(circirW, circirW), NO, 0.0);// 画大圆UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, circirW, circirW)];// 获取当前上下文CGContextRef ctx = UIGraphicsGetCurrentContext();// 添加到上下文CGContextAddPath(ctx, path.CGPath);// 设置颜色[color set];// 渲染CGContextFillPath(ctx);CGRect clipR = CGRectMake(borderW, borderW, oldImage.size.width, oldImage.size.height);// 画圆:正切于旧图片的圆UIBezierPath *clipPath = [UIBezierPath bezierPathWithOvalInRect:clipR];// 设置裁剪区域[clipPath addClip];// 画图片[oldImage drawAtPoint:CGPointMake(borderW, borderW)];// 获取新的图片UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();// 关闭上下文UIGraphicsEndImageContext();return newImage;}
12、屏幕截屏
12345678910111213141516171819 // 开启上下文UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0.0);// 获取上下文CGContextRef ctx = UIGraphicsGetCurrentContext();// 渲染控制器view的图层到上下文// 图层只能用渲染不能用draw[self.view.layer renderInContext:ctx];// 获取截屏图片UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();// 关闭上下文UIGraphicsEndImageContext();NSData *data = UIImagePNGRepresentation(newImage);[data writeToFile:@"/Users/apple/Desktop/layer.png" atomically:YES];
13、给图片添加水印
UIImage *oldImage = [UIImage imageNamed:@"img"]; // 开启上下文 // size 新的图片大小 // opaque YES 不透明 NO 透明 UIGraphicsBeginImageContextWithOptions(oldImage.size, NO, 0.0); [oldImage drawAtPoint:CGPointZero]; NSString *text = @"我要高薪 !"; NSDictionary *dict = @{NSFontAttributeName : [UIFont systemFontOfSize:15],NSForegroundColorAttributeName : [UIColor redColor]}; [text drawAtPoint:CGPointMake(120, 170) withAttributes:dict]; // 获取新的图片 UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); // 关闭上下文 UIGraphicsEndImageContext(); _imageView.image = newImage; // 把图片转换成png格式的二进制数据 NSData *data = UIImagePNGRepresentation(newImage); // 写入桌面 [data writeToFile:@"/Users/apple/Desktop/newImage.png" atomically:YES];
14.其他
- 画路线是不用获取context
- 注意:如果view类中没有设置过backgroundcolor,再在drawRect中绘制就会重叠。或者更改颜色,如:[[UIColor whiteColor] set];也会有问题。先设置一下该view的backgroundcolor问题解决了。