如何在客观c中平滑地绘制自由手线?

时间:2012-06-18 07:37:10

标签: objective-c

我使用uigraphics cgcontext在触摸上绘制线条...但我的线条不平滑我的意思是边缘不光滑所以任何人都可以建议我如何画出流畅的线条。 这里是我用来画线的代码:

/* 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);`

这是我的代码,我可以画线自由,但它不是那么平滑显示像素我的意思是边缘不光滑

3 个答案:

答案 0 :(得分:5)

这个代码由kyoji发布完美。您可以阅读问题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();

}

答案 1 :(得分:1)

使用点不流畅和流畅。你应该使用NSBeziePath ...

它花了3000行,但非常光滑,作为“竹子”工具。

正确的方法是使用Bezier路径:

  • 记录每张图纸
  • 用于在视图上有效绘制
  • 你应该在跟踪时计算bezier路径的动态锚点(你不绘制事件的接触点)
  • 您甚至可以录制并保存。

答案 2 :(得分: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;
}