Quartz2D知识点聚合
基本
代码语言:javascript
复制 //画图片
UIImage *image = [UIImage imageNamed:@"阿狸头像"];
[image drawInRect:rect];
//字体
NSString *title = @"标题";
NSMutableDictionary *atr = [NSMutableDictionary dictionary];
atr[NSFontAttributeName] = [UIFont systemFontOfSize:15];
// atr[NSForegroundœColorAttributeName] = [UIColor greenColor];
[title drawInRect:CGRectMake(120, 20, 100, 20) withAttributes:atr];
//椭圆
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(10, 20, 70, 130)];
[path stroke];
//方形
UIBezierPath *path1 = [UIBezierPath bezierPathWithRect:CGRectMake(10, 200, 10, 50)];
[path1 stroke];
//圆角方形
UIBezierPath *path2 = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(100, 30, 100, 100) cornerRadius:10];
[path2 stroke];
//一个角圆角
UIBezierPath *path3 = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(210, 90, 80, 70) byRoundingCorners:UIRectCornerTopRight cornerRadii:CGSizeMake(20, 30)];
[path3 stroke];
//圆弧
UIBezierPath *path4 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(130, 230) radius:70 startAngle:0 endAngle:M_PI clockwise:YES];
[path4 stroke];
代码语言:javascript
复制// 1.获得当前上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 2.拼接路径
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(10, 20)];
[path addQuadCurveToPoint:CGPointMake(200, 80) controlPoint:CGPointMake(100, 200)];
// 3. 添加路径到上下文
CGContextAddPath(ctx, path.CGPath);
// 4.渲染上下文
CGContextStrokePath(ctx);
变换
代码语言:javascript
复制 //变换
//平移
// CGContextTranslateCTM(ctx, 10, 20);
//旋转
CGContextRotateCTM(ctx, M_PI_4);
//缩放
CGContextScaleCTM(ctx, 1.2, 1.2);
上下文栈
代码语言:javascript
复制// 1.获得当前上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 2.拼接路径
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(10, 20)];
[path addQuadCurveToPoint:CGPointMake(200, 80) controlPoint:CGPointMake(100, 200)];
// 3. 添加路径到上下文
CGContextAddPath(ctx, path.CGPath);
//保存上下文
CGContextSaveGState(ctx);
//设置上下文状态
CGContextSetLineWidth(ctx, 10);
[[UIColor redColor] set];
// 4.渲染上下文
CGContextStrokePath(ctx);
// 2.拼接路径
path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(100, 80)];
[path addLineToPoint:CGPointMake(200, 200)];
// 3. 添加路径到上下文
CGContextAddPath(ctx, path.CGPath);
//还原上下文
CGContextRestoreGState(ctx);
//设置上下文状态
CGContextSetLineWidth(ctx, 5);
[[UIColor blueColor] set];
// 4.渲染上下文
CGContextStrokePath(ctx);
生成图片
代码语言:javascript
复制 UIImage *image = [UIImage imageNamed:@"小黄人"];
UIGraphicsBeginImageContextWithOptions(image.size, YES, 0);
[image drawAtPoint:CGPointZero];
NSString *str = @"小黄人";
[str drawAtPoint:CGPointZero withAttributes:nil];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
截图
代码语言:javascript
复制 //开启图片上下文
UIGraphicsBeginImageContext(view.frame.size);
//获得当前上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//给定裁减区域-----
//渲染图片
[view.layer renderInContext:ctx];
//从当前上下文得到一张图片
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
//关闭图片上下文
UIGraphicsEndImageContext();
return image;
擦除
代码语言:javascript
复制 UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.imageView];
//开启上下文
UIGraphicsBeginImageContextWithOptions(self.imageView.frame.size, NO, 0);
//获得当前上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//渲染到上下文
[self.imageView.layer renderInContext:ctx];
//获取擦除区域
CGRect rect = CGRectMake(point.x - 10, point.y - 10, 20, 20);
// 擦除上下文的内容
CGContextClearRect(ctx, rect);
// 生成图片
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
_imageView.image = image;
// 关闭上下文
UIGraphicsEndImageContext();