2012-02-16 56 views
0

enter image description here目標C重新調整繪圖區域

我想我的繪圖區域調整到一個更小的尺寸,但每當我做,我遇到這種輸出的(見照片)。

當我開始寫它不繪製一條線,只是延伸一切。

我想弄清楚是什麼問題,但我知道是不夠的。

這裏是我的代碼:

viewDidLoad中:

touchDraw = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"opaque.png"]]; 
    touchDraw.frame = CGRectMake(0, 0, 768, 1024); 
    [self.view addSubview:touchDraw]; 
    [self.view sendSubviewToBack:touchDraw]; 
    touchMoved = 0; 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    touchSwiped = NO; 
    UITouch *touch = [touches anyObject]; 

    endingPoint = [touch locationInView:self.view]; 
    endingPoint.y -= 35; 

} 

亮點:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    touchSwiped = YES; 
    UITouch *touch = [touches anyObject]; 
    currentTouch = [touch locationInView:self.view]; 
    currentTouch.y -= 35; 
    UIGraphicsBeginImageContext(self.view.frame.size); 
    [touchDraw.image drawInRect:CGRectMake(0, 0, 768, 1024)]; 
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
    UIImage * textureColor = [UIImage imageNamed:@"Brush_Black.png"]; 

    CGPoint vector = CGPointMake(currentTouch.x - endingPoint.x, currentTouch.y - endingPoint.y); 
    CGFloat distance = hypotf(vector.x, vector.y); 
    vector.x /= distance; 
    vector.y /= distance; 
    for (CGFloat i = 0; i < distance; i += 1.0f) { 
     CGPoint p = CGPointMake(endingPoint.x + i * vector.x, endingPoint.y + i * vector.y); 
     [textureColor drawAtPoint:p blendMode:blendMode alpha:0.5f]; 
    } 
    CGContextBeginPath(UIGraphicsGetCurrentContext()); 
    CGContextStrokePath(UIGraphicsGetCurrentContext()); 
    touchDraw.image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    endingPoint = currentTouch; 

    touchMoved++; 

    if (touchMoved == 1) { 
     touchMoved = 0; 
    } 

} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    if(!touchSwiped) { 
     UIGraphicsBeginImageContext(self.view.frame.size); 
     [touchDraw.image drawInRect:CGRectMake(0, 0, touchDraw.frame.size.width, touchDraw.frame.size.height)]; 
     CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
     CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 25.0); 
     UIImage * textureColor = [UIImage imageNamed:@"Brush_Black.png"]; 
     [textureColor drawAtPoint:CGPointMake(endingPoint.x, endingPoint.y) blendMode:blendMode alpha:1.0f]; 
     CGContextStrokePath(UIGraphicsGetCurrentContext()); 
     CGContextFlush(UIGraphicsGetCurrentContext()); 
     touchDraw.image = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
    } 

} 
+0

您是否在此項目中實施了撤銷操作?如果是這樣,請發佈您的代碼 – vinay 2012-03-22 08:34:49

回答

1

改變這一行

UIGraphicsBeginImageContext(self.view.frame.size); 

,因爲這

UIGraphicsBeginImageContext(touchDraw.frame.size); 

這可能有所幫助。

+0

它的工作原理,但是當我嘗試更改其位置時,它會再次提供拉伸輸出 – Crisn 2012-02-16 07:07:09

+0

@Crisn。改變什麼位置?請澄清。 – Vignesh 2012-02-16 07:20:48

+0

現在沒事了!謝謝!我只是改變觸摸位置! – Crisn 2012-02-16 07:35:47