zoukankan      html  css  js  c++  java
  • iOS Quartz2D裁剪图片

    1. 对上面的图片做裁剪:

        CGContextRef ctx = UIGraphicsGetCurrentContext();

        // 画圆

        CGContextAddEllipseInRect(ctx, CGRectMake(50, 50, 60, 60));

        // 圆外的都剪掉

        CGContextClip(ctx);

        CGContextFillPath(ctx);

        

        // 画图

        UIImage *image = [UIImage imageNamed:@"123"];

        [image drawAtPoint:CGPointMake(15, 15)];

    2. 模拟器运行结果:

    3. 再画一条线,要求不要裁剪线:

        CGContextRef ctx = UIGraphicsGetCurrentContext();

        // 画圆

        CGContextAddEllipseInRect(ctx, CGRectMake(50, 50, 60, 60));

        // 圆外的都剪掉

        CGContextClip(ctx);

        CGContextFillPath(ctx);

        // 画图

        UIImage *image = [UIImage imageNamed:@"123"];

        [image drawAtPoint:CGPointMake(15, 15)];

        // 画线

        CGContextSetLineWidth(ctx, 10);

        CGContextMoveToPoint(ctx, 20, 80);

        CGContextAddLineToPoint(ctx, 120, 80);

        CGContextStrokePath(ctx);

    4. 出现这种情况的原因:

    UIView上有图层(图1,图2),Quartz2D的所有操作都是在图层上进行的,在图层上画一个圆(图3),然后做Clip,再画一条线(图4),实际得到结果会是图5的样子,因为圆外的区域都被剪掉了

    5. 正确的做法是利用图形上下文栈:

        CGContextRef ctx = UIGraphicsGetCurrentContext();

        CGContextSaveGState(ctx);

        // 画圆

        CGContextAddEllipseInRect(ctx, CGRectMake(50, 50, 60, 60));

        // 圆外的都剪掉

        CGContextClip(ctx);

        CGContextFillPath(ctx);

        // 画图

        UIImage *image = [UIImage imageNamed:@"123"];

        [image drawAtPoint:CGPointMake(15, 15)];    

        CGContextRestoreGState(ctx);

        // 画线

        CGContextSetLineWidth(ctx, 10);

        CGContextMoveToPoint(ctx, 20, 80);

        CGContextAddLineToPoint(ctx, 120, 80);

        CGContextStrokePath(ctx);

  • 相关阅读:
    MySQL常用函数
    SQL之join
    java并发编程之三--CyclicBarrier的使用
    java并发编程之二--CountDownLatch的使用
    java并发编程之一--Semaphore的使用
    微信小程序 bindcontroltap 绑定 没生效
    报错:Syntax error on tokens, delete these tokens
    java创建类的5种方式
    数据类型转换
    JS数据结构算法---数组的算法
  • 原文地址:https://www.cnblogs.com/oumygade/p/4360133.html
Copyright © 2011-2022 走看看