2012-06-18 49 views
2

我已經使用uigraphics cgcontext畫線觸摸...但我的線不光滑我的意思是邊緣不光滑,所以任何人都可以建議我如何繪製流暢的線.. ?? 這裏我的代碼是我用來畫線:如何在目標c中順暢地繪製自由手線?

/* touchesBegan */ 
UITouch *touch = [touches anyObject]; 
lastPoint = [touch locationInView:imagevw]; 

/* touchesMoved */ 

CGFloat width = [mySlider value]; 
UIGraphicsBeginImageContext(imagevw.frame.size); 
[imagevw.image drawInRect:CGRectMake(0, 0, imagevw.frame.size.width,imagevw.frame.size.height)]; 
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), width); 
UITouch *touch = [touches anyObject]; 
mouseSwiped = YES; 
    CGPoint currentPoint = [touch locationInView:imagevw]; 
    CGContextBeginPath(UIGraphicsGetCurrentContext()); 
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
    CGContextStrokePath(UIGraphicsGetCurrentContext()); 
    imagevw.image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
lastPoint = currentPoint; 





/* touchesEnded */ 
/* same here */ 
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
     CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);` 

這是我的代碼,我可以畫線放手,但它不是平滑的顯示像素我的意思是邊緣不光滑

回答

4

該代碼張貼京都完美的作品。您可以閱讀問題here

CGPoint midPoint(CGPoint p1, CGPoint p2) 
{ 

    return CGPointMake((p1.x + p2.x) * 0.5, (p1.y + p2.y) * 0.5); 

} 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

    UITouch *touch = [touches anyObject]; 

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

} 

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

    UITouch *touch = [touches anyObject]; 

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


    // calculate mid point 
    CGPoint mid1 = midPoint(previousPoint1, previousPoint2); 
    CGPoint mid2 = midPoint(currentPoint, previousPoint1); 

    UIGraphicsBeginImageContext(self.imageView.frame.size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    [self.imageView.image drawInRect:CGRectMake(0, 0, self.imageView.frame.size.width, self.imageView.frame.size.height)]; 

    CGContextMoveToPoint(context, mid1.x, mid1.y); 
    // Use QuadCurve is the key 
    CGContextAddQuadCurveToPoint(context, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y); 

    CGContextSetLineCap(context, kCGLineCapRound); 
    CGContextSetLineWidth(context, 2.0); 
    CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0); 
    CGContextStrokePath(context); 

    self.imageView.image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

} 
+0

我已經試過這仍然這不是光滑....如果你知道任何其他可以請你寄給我,如果任何示例代碼將是非常有益的。感謝advanc .. –

0

不流暢而平滑使用點..你應該使用NSBeziePath ...

花了3000線,但很流暢,「竹子」的工具。

正確的做法是使用貝塞爾路徑:

  • 記錄每個圖紙
  • 用於對視圖
  • 有效地吸引你應該計算在飛錨點貝塞爾路徑,並同時追蹤(你不會畫出事件的觸點)
  • 你甚至可以記錄並保存它。
1
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    pointCurrent = [touch locationInView:self.view]; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint pointNext = [touch locationInView:self.view]; 
    UIGraphicsBeginImageContext(img.frame.size); 
    [img.image drawInRect:CGRectMake(0, 0, img.frame.size.width, img.frame.size.height)]; 
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2.0); 
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0); 
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), pointCurrent.x, pointCurrent.y); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), pointNext.x, pointNext.y); 
    CGContextStrokePath(UIGraphicsGetCurrentContext()); 
    img.image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    pointCurrent = pointNext; 
}