【问题标题】:Clipping inverse drawing?剪裁逆图?
【发布时间】:2012-11-13 06:33:36
【问题描述】:

我想删除以下图片的蓝色部分。如何剪裁图形的反面? (我相信这是提问的正确方式)

三角形的示例代码:(如果有更好的三角形代码,我也会接受!;)

    int lineWidth = 4;

    int w = size.size.width;
    int h = size.size.height - lineWidth;

    CGPoint top = CGPointMake(0+(w/2)+.5, 0);

    CGContextClipToRect(ctx, CGRectMake(0, 0, w, h));
    CGContextStrokePath(ctx);

    CGContextMoveToPoint(ctx, top.x, top.y);
    CGContextAddLineToPoint(ctx, top.x + (w/2), top.y + h  );
    CGContextAddLineToPoint(ctx, top.x - (w/2), top.y + h   );
    CGContextAddLineToPoint(ctx, top.x, top.y);

    CGContextFillPath(ctx);

    CGContextSetLineWidth(ctx, lineWidth);
    CGContextSetLineCap(ctx, kCGLineCapRound);
    CGContextMoveToPoint(ctx, top.x, top.y);
    CGContextAddLineToPoint(ctx, top.x + (w/2), top.y + h  );
    CGContextAddLineToPoint(ctx, top.x - (w/2), top.y + h   );
    CGContextAddLineToPoint(ctx, top.x, top.y);

    CGContextStrokePath(ctx);

【问题讨论】:

    标签: iphone geometry drawing core-graphics clipping


    【解决方案1】:

    一个简单的解决方案是使用 UIBazierPath。根据您的意图绘制一条 Bazier 路径。然后调用 [path addClip] 方法。它会剪掉封闭路径之外的所有内容。
    例如,下面的代码会使您的视图变成圆角。

    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                               byRoundingCorners:UIRectCornerAllCorners
                                                     cornerRadii:CGSizeMake(6.0, 6.0)];
    [path addClip];
    

    【讨论】:

      【解决方案2】:

      要剪辑任何路径的反转,只需在剪辑中添加一个封闭的矩形。

      在这种情况下,如果我没猜错的话,您希望围绕形状的红色笔划进行剪辑。通常,由于描边路径的线宽,这在石英中并不是一件容易的事。当然,对于这两个形状,我们可以计算红色笔触的轮廓并围绕它进行裁剪。但是一个通用的解决方案需要一种方法来计算包含的路径,包括线宽。

      还有其他包含位图蒙版的方法,但最佳解决方案取决于您绘制的具体内容。我刚刚遇到了一个类似的问题,我可以使用专门的石英混合模式非常优雅有效地解决它。这在很大程度上取决于绘制前 centtext 的内容以及结果应如何与 UI 的其余部分混合。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-05-10
        • 1970-01-01
        • 2022-01-23
        • 2015-02-02
        • 2017-05-22
        • 1970-01-01
        • 2023-04-02
        相关资源
        最近更新 更多