iOS:围绕绘制路径裁剪?

时间:2015-09-18 02:13:32

标签: ios core-graphics

我正在使用ACEDrawingView在视图中绘制。

enter image description here

我如何检测绘图的宽度和高度,以便我可以围绕它进行裁剪,如下所示:

enter image description here

更新:在@Duncan指出正确的方向后,我能够查看源代码并找到以下内容:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    // save all the touches in the path
    UITouch *touch = [touches anyObject];

    previousPoint2 = previousPoint1;
    previousPoint1 = [touch previousLocationInView:self];
    currentPoint = [touch locationInView:self];

    if ([self.currentTool isKindOfClass:[ACEDrawingPenTool class]]) {
        CGRect bounds = [(ACEDrawingPenTool*)self.currentTool addPathPreviousPreviousPoint:previousPoint2 withPreviousPoint:previousPoint1 withCurrentPoint:currentPoint];

        CGRect drawBox = bounds;
        drawBox.origin.x -= self.lineWidth * 2.0;
        drawBox.origin.y -= self.lineWidth * 2.0;
        drawBox.size.width += self.lineWidth * 4.0;
        drawBox.size.height += self.lineWidth * 4.0;
        self.drawingBounds = bounds; // I added this property to allow me to extract the bounds and use it in my view controller

        [self setNeedsDisplayInRect:drawBox];
    }
    else if ([self.currentTool isKindOfClass:[ACEDrawingTextTool class]]) {
        [self resizeTextViewFrame: currentPoint];
    }
    else {
        [self.currentTool moveFromPoint:previousPoint1 toPoint:currentPoint];
        [self setNeedsDisplay];
    }

}

但是当我测试边界时,我得到了这个:

enter image description here

我会继续努力解决这个问题,但如果有人能提供帮助那就太棒了!

更新3:使用CGContextGetPathBoundingBox我终于能够实现它了。

enter image description here

2 个答案:

答案 0 :(得分:1)

我不熟悉AceDrawingView类。我可以告诉你如何使用iOS框架:

将路径创建为UIBezierPath。

询问路径的bounds属性。

答案 1 :(得分:1)

每次获得if时,都会记录您正在绘制的点的位置。当你们都完成了,你就拥有了所有的观点。现在查看所有这些点中的最大x值和最小x值以及最大y值和最小y值。这是图纸的边界框。

另一种方法(您已经发现)是保存CGPath然后调用CGContextGetPathBoundingBox。基本上,这完全是一回事。

enter image description here

请注意,路径没有厚度,而中风则没有。你需要负面地插入边界框以允许这个(我的截屏视频不会这样做)。