如何在手写时创建线条曲线

时间:2012-02-08 19:37:37

标签: core-graphics handwriting cgcontextdrawimage

我正在创建一个应用程序,在一个部分中,我正在尝试实现手写。我已经使用了一个imageView来写,它将被保存并作为pdf发送到服务器。我已经实现了touch begin,move和end,并且使用了contextAddLineToPoint,我可以在用户编写内容时创建行。然而。写作有点尖,我试图在用户改变写作方向时创建曲线,即写入字母时。我真的不想实现手写识别,只是为了平滑所绘制的线条。

我创建了一个“缓冲区”,由一个数组组成,当用户移动触摸时,它保存两个中间点。如下:

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

UITouch *touch = [touches anyObject];

lastPoint = [touch locationInView:drawImage];
NSLog(@"first touch:%@",[NSValue valueWithCGPoint:lastPoint]);

}

drawImage是我用来在btw上写的imageView。然后去触摸移动:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];   
currentPoint = [touch locationInView:drawImage];

if (movementCount<2) {
    [pointHolderArray addObject:[NSValue valueWithCGPoint:currentPoint]];
    movementCount ++;
    NSLog(@"pointHolderArray: %@",pointHolderArray);
}else
{      
    NSLog(@"buffer full");
    UIGraphicsBeginImageContext(drawImage.frame.size);
    [drawImage.image drawInRect:drawImage.frame];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), YES);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 3.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.3, 0.5, 0.2, 1.0);
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(),[[pointHolderArray objectAtIndex:0]CGPointValue].x,[[pointHolderArray objectAtIndex:0]CGPointValue].y);
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), [[pointHolderArray objectAtIndex:0]CGPointValue].x,[[pointHolderArray objectAtIndex:0]CGPointValue].y); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), [[pointHolderArray objectAtIndex:1]CGPointValue].x,[[pointHolderArray objectAtIndex:1]CGPointValue].y);
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), [[pointHolderArray objectAtIndex:1]CGPointValue].x,[[pointHolderArray objectAtIndex:1]CGPointValue].y); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    lastPoint = [touch previousLocationInView:drawImage];
    [pointHolderArray removeAllObjects];
    movementCount=0;
}

}

正如您所看到的,每次存储两个点时,我就会在它们之间绘制线条。这使得绘图稍微变硬,而且线条更加粗糙。

任何人都可以帮助解决这个问题,我对iOS中的图形非常陌生,并且不确定我是否使用了正确的API,如果我甚至应该使用imageView。

非常感谢

1 个答案:

答案 0 :(得分:1)

如何使用Pan Gesture Recognizer和GLPaint。你在viewDidLoad中设置了这样的东西:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIPanGestureRecognizer *g = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(viewPanned:)];

..

然后,捕捉平移动作:

-(void) viewPanned:(UIPanGestureRecognizer *)g 
{

    CGPoint point = [g locationInView:paintView];
    // invert y for OpenGL
    point.y = paintView.bounds.size.height - point.y ;

    switch (g.state) {
        case UIGestureRecognizerStateBegan:
            prevPoint = point ;
            break ;
        case UIGestureRecognizerStateChanged:
            [paintView renderLineFromPoint:prevPoint toPoint:point] ;
            prevPoint = point ;
            break ;
        case UIGestureRecognizerStateEnded:
            prevPoint = point ;
            break ;
    }
}

此处显示的'paintView'是PaintView的实例,您可以在Apple示例代码的GLPaint示例中找到它。该示例未显示如何更改笔的大小,但您可以通过设置各种glPointSize来完成。

- (void)setBrushSize:(CGFloat)size 
{ 
  if( size <= 1.0 ) {
        glPointSize(10);
    }
    else if ( size <= 2.0 ) {
        glPointSize(20);
    }
    else if ( size <= 3.0 ) {
        glPointSize(30);
    }    
    else if ( size <= 4.0 ) {
        glPointSize(40);
    }    
    ..

希望这会有所帮助..