2013-07-31 39 views
1

我有代碼,讓我畫上一個UIImageView 的頂部 - 但我希望能夠限制繪圖區域。iOS版:重新定位UIGraphicsBeginImageContext和UIImageView的

我已經能夠限制大小,但現在我無法將它定位:如果我改變(0,0)到別的我的形象完全消失,並提請停止工作的能力

drawImage方法 - 是一個UIImageView

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    mouseSwiped = YES; 

    UITouch *touch = [touches anyObject]; 
    currentPoint = [touch locationInView:self]; 

    UIGraphicsBeginImageContext(CGSizeMake(560, 660)); 
    [drawImage.image drawInRect:CGRectMake(0, 0, 560, 660)]; 

    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0); 
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0, 1, 0, 1); 
    CGContextBeginPath(UIGraphicsGetCurrentContext()); 
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
    CGContextStrokePath(UIGraphicsGetCurrentContext()); 

    [drawImage setFrame:CGRectMake(0, 0, 560, 660)]; 
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    lastPoint = currentPoint; 

    [self addSubview:drawImage]; 
} 

任何幫助是非常讚賞

回答

1

我不知道我完全理解你想要做什麼,但我會採取搖擺:

[drawImage.image drawInRect:CGRectMake(0, 0, 560, 660)]; 

這告訴分配給drawImage的圖像在0,0的當前上下文中繪製自己。由於上下文是位圖圖像上下文,因此其範圍將從0,0560,660。在我看來,這可能是你想要做的,否則你的位圖會有空白區域。如果您要將0,0更改爲10,0,我預計在結果位圖中會出現10pt寬的空白區域(這看起來很浪費)。

再後來你這樣做:

[drawImage setFrame:CGRectMake(0, 0, 560, 660)]; 

這是在它的父座標系統設置的UIImageView座標。如果你將0,0改爲5,5我會期望你的UIImageView在其超視圖中出現5pt和5pt。

你說你想「限制繪圖區域」。我假設你是指你在圖像上繪製的部分。最簡單的方法是在繪製之前在位圖上下文中設置剪輯矩形。舉例來說,如果你想防止圖中圍繞圖像邊緣的10PT帶,你能做到這一點是這樣的:

UIGraphicsBeginImageContext(CGSizeMake(560, 660)); 
[drawImage.image drawInRect:CGRectMake(0, 0, 560, 660)]; 

CGContextClipToRect(UIGraphicsGetCurrentContext(), CGRectMake(10,10,540,640)); 
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
// ... and so on 

不是100%肯定,如果這就是你要找的,但希望它幫助。

+0

坦克了很多的解釋: –

1

試圖通過限制currentPoint

if (currentPoint.x <= 20 || currentPoint.x >= 280 ||  
    currentPoint.y <= 20 || currentPoint.y >= 400) 
{ 
    lastPoint = currentPoint; 
    return; 
}