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

  • 相关阅读:
    在.net中过滤敏感字符
    const和readonly的联系和区别
    新闻内容页分页的简单做法
    jmail邮件发送问题
    nvarchar与varchar的区别
    C#中"is" vs "as"
    在C#中,string 类型可与SQL SERVER中的int 类型作比较
    做网站常用代码集锦 (转)
    做网站常用代码集锦 (转)
    ADO 数据类型转换表 oledbtype sqldbtype (二)
  • 原文地址:https://www.cnblogs.com/oumygade/p/4360133.html
Copyright © 2011-2022 走看看