前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Quartz2D知识点聚合案例

Quartz2D知识点聚合案例

作者头像
用户1941540
发布2018-05-11 13:42:46
5670
发布2018-05-11 13:42:46
举报
文章被收录于专栏:ShaoYL

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();
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2015-07-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Quartz2D知识点聚合
  • 基本
  • 变换
  • 上下文栈
  • 生成图片
  • 截图
  • 擦除
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档