CGContextRef CGMutablePathRef UIBezierPath

直接上代码


用三种方式画个笑脸

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //用三种方法画脸,
    //1.用CGMutablePathRef
    [self paintFaceWithCGPath];

    //2.完全用CGContextRef
    //这些函数执行起来其实是和CGPath完全等价的。
    //这里需要注意的是,完全使用CGContextRef的话,Transform的应用需使用CGContextTranslateCTM函数。
    [self paintFaceWithCGContextRef];

    //3.使用贝塞尔曲线
//    使用UIBezierPath类型来完成上述图形,UIBezierPath很有意思,它包装了Quartz的相关API,自己存在于UIKit中,因此不是基于C的API,而是基于Objective-C对象的。那么一个非常重要的点是由于离开了Quartz绘图,所以不需要考虑Y轴翻转的问题,在画弧的时候,clockwise参数是和现实一样的,如果需要顺时针就传YES,而不是像Quartz环境下传NO的。
//    
//    其次椭圆的创建需使用bezierPathWithOvalInRect方法,这里名字是Oral而不是Quartz中的Ellipse。
//    
//    最后注意UIBezierPath的applyTransform方法需要最后调用。

    [self paintFaceWithBezier];

    //总结:
//    其实都是围绕context来进行绘制,不管用哪种方式,最后都是用context的到image图像
- (void)paintFaceWithCGPath{
    //开始图像绘制,设置上下文
    UIGraphicsBeginImageContext(self.view.bounds.size);
    //获取到上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //创建用于转移坐标的Transform,这样我们不用按照实际转移坐标计算
    CGAffineTransform tranform = CGAffineTransformMakeTranslation(30.0, 50.0f);
    //创建CGMutablePathRef
    CGMutablePathRef path = CGPathCreateMutable();
    //绘制左眼
    CGPathAddEllipseInRect(path, &tranform, CGRectMake(0, 0, 20, 20));
    //绘制右眼
    CGPathMoveToPoint(path, &tranform, 100, 10);
    CGPathAddArc(path, &tranform, 90.0f, 10.0f, 10, 0, M_PI * 2, true);
    //笑
    CGPathMoveToPoint(path, &tranform, 100, 50);
    //这个点的坐标是圆心的位置
    CGPathAddArc(path, &tranform, 50, 50, 50, 0, M_PI, false);
    //将path添加到context内
    CGContextAddPath(ctx, path);

    //设置绘图属性
    [[UIColor blueColor] setStroke];
    CGContextSetLineWidth(ctx, 2);
    //执行绘画
    CGContextStrokePath(ctx);

    //从context上获取图像,显示在view上
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    //结束绘画,释放context
    UIGraphicsEndImageContext();
    CGPathRelease(path);
    CGContextRelease(ctx);
    UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
    [self.view addSubview:imageView];
}
- (void)paintFaceWithCGContextRef{
    //设置画布(同上)
    UIGraphicsBeginImageContext(self.view.bounds.size);
    //拿到context,也可以说是拿到画笔
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //使用CGContextTranslateCTM来设置画的图像在画布的偏移 ,不用计算绝对坐标,只需要算相对坐标
    CGContextTranslateCTM(ctx, 170, 50);
    //左眼
    CGContextAddEllipseInRect(ctx, CGRectMake(0, 0, 20, 20));
    //右眼
    CGContextMoveToPoint(ctx, 100, 10);
    CGContextAddArc(ctx, 90, 10, 10, 0, M_PI * 2, YES);
    //笑
    CGContextMoveToPoint(ctx, 100, 50);
    CGContextAddArc(ctx, 50, 50, 50, 0, M_PI, NO);
    //设置绘图属性
    [[UIColor redColor] setStroke];
    CGContextSetLineWidth(ctx, 2);
    //执行绘画
    CGContextStrokePath(ctx);

    //获取图像,显示在界面上
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    CGContextRelease(ctx);
    UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
    [self.view addSubview:imageView];

}
//最后注意UIBezierPath的applyTransform方法需要最后调用。
//贝赛尔曲线是画在layer层的
- (void)paintFaceWithBezier{
    //开始绘制,设置画布
    UIGraphicsBeginImageContext(self.view.bounds.size);
    //创建bezierPath
    UIBezierPath *path = [UIBezierPath bezierPath];
    //左眼
    [path appendPath:[UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 20, 20)]];
    //右眼
    [path appendPath:[UIBezierPath bezierPathWithArcCenter:CGPointMake(90, 10) radius:10 startAngle:0 endAngle:M_PI * 2 clockwise:YES]];
    //笑
//    [path moveToPoint:CGPointMake(100, 50)];
//    注意这里clockwise参数是YES而不是NO,因为这里不知Quartz,不需要考虑Y轴翻转的问题
    [path appendPath:[UIBezierPath bezierPathWithArcCenter:CGPointMake(50, 50) radius:50 startAngle:0 endAngle:M_PI clockwise:YES]];

    [path applyTransform:CGAffineTransformMakeTranslation(100, 200)];
    //设置绘画属性
    [[UIColor greenColor] setStroke];
    [path setLineWidth:2];

    [path stroke];

    //从context获取图像,显示在界面上
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
    [self.view addSubview:imageView];

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值