Quartz2D知识点聚合案例

转载自:http://www.cnblogs.com/ShaoYinling/p/4649718.html


Quartz2D知识点聚合

基本

   //画图片
  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];

// 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);

变换

  //变换
  //平移
//	CGContextTranslateCTM(ctx, 10, 20);
  //旋转
  CGContextRotateCTM(ctx, M_PI_4);
  //缩放
  CGContextScaleCTM(ctx, 1.2, 1.2);

上下文栈

  • 先保存或者还原上下文栈,再设置状态
// 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);

生成图片

  UIImage *image = [UIImage imageNamed:@"小黄人"];
  UIGraphicsBeginImageContextWithOptions(image.size, YES, 0);
  [image drawAtPoint:CGPointZero];
  NSString *str = @"小黄人";
  [str drawAtPoint:CGPointZero withAttributes:nil];
  image = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();

截图

  • 给定裁减区域再渲染
  //开启图片上下文
  UIGraphicsBeginImageContext(view.frame.size);
  //获得当前上下文
  CGContextRef ctx = UIGraphicsGetCurrentContext();
  //给定裁减区域-----
  //渲染图片
  [view.layer renderInContext:ctx];
  //从当前上下文得到一张图片
  UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  //关闭图片上下文
  UIGraphicsEndImageContext();
  return image;

擦除

  • 先渲染到上下文,再擦除
 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();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值