2015-09-18 64 views
0

我使用ACEDrawingView在視圖內繪製。iOS:圍繞繪製的路徑裁剪?

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

回答

1

每當您獲得touchesMoved時,請記錄您現在繪製的點的位置。當你全部完成時,你有所有的要點。現在看所有這些點中最大的x值和最小的x值以及最大的y值和最小的y值。這是繪圖的邊界框。

另一種方法(您已經發現)是保存CGPath,然後調用CGContextGetPathBoundingBox。基本上,這是完全一樣的事情。

enter image description here

注意的路徑沒有厚度,而您的行程一樣。你需要消極地插入邊界框來允許這個(我的屏幕截圖沒有這樣做)。

+0

謝謝馬特!我實際上找到了一個'CGMutablePathRef path'實例變量,並使用'return CGPathGetBoundingBox(path);'我能夠得到它! – KingPolygon

+0

糟糕,我的方法似乎只得到最新的路徑。所以,如果我繪製多條線(繪製,提起手指和繪製),它只會看到更新的路徑。我會給你的方法去。 – KingPolygon

+0

哦,只需保留所有路徑並製作複合路徑並獲取該路徑的邊界框即可。我的答案是不必要的。你的方式是最好的。 – matt

1

我不熟悉的AceDrawingView類。我可以告訴你如何使用iOS框架:

創建您的路徑作爲UIBezier路徑。

詢問路徑的邊界屬性。

+0

謝謝你指點我在正確的方向鄧肯。我正在瀏覽ACEDrawingView的源代碼,我想我可能會越來越近。我會更新我的答案。 – KingPolygon