0

I have planned to do simple drawing up, using that use can draw straight lines and free hand drawing, based on the selection it change. But the problem is straight line gets repeated from the starting point more than 10 or 15 lines when i try to draw 1 line, free hand drawing working fine.I have posted my code here. Please give your suggestions to fix this issue.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    mouseSwiped = NO;
    UITouch *touch = [touches anyObject];
    previousPoint1 = [touch previousLocationInView:self.view];
    previousPoint2 = [touch previousLocationInView:self.view];
    currentPoint = [touch locationInView:self.view];
    lastPoint = [touch locationInView:self.view];
}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    // Work download Flag
        _isWorkModified = YES;

    mouseSwiped = YES;

    UITouch *touch = [touches anyObject];
        previousPoint2 = previousPoint1;
    previousPoint1 = [touch previousLocationInView:self.view];
    currentPoint = [touch locationInView:self.view];

    UIGraphicsBeginImageContext(self.view.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

    if(_selectedShape == 3)
    {
        // calculate mid point
        CGPoint mid1 = [self midPoint:previousPoint1 :previousPoint2];
        CGPoint mid2 = [self midPoint:currentPoint :previousPoint1];

        CGContextMoveToPoint(context, mid1.x, mid1.y);
        CGContextAddQuadCurveToPoint(context, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y);
        CGContextSetLineCap(context, kCGLineCapRound);
        if(self.isEraseButtonSelected)
        {
            CGContextSetLineWidth(context, 40.0);
            CGContextSetRGBStrokeColor(context, 0.823,0.886,0.898,1.0);
        }
        else
        {
            CGContextSetLineWidth(context, 3.0);
            CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0);
        }
        CGContextStrokePath(context);
    }
    else if(_selectedShape == 0)
    {
        CGContextSetLineWidth(context, 3.0);
        CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0);
        CGContextBeginPath(context);
        CGContextMoveToPoint(context, lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(context, currentPoint.x, currentPoint.y);

        CGContextClosePath(context);
        CGContextStrokePath(context);

    }
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

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

        NSLog(@"touch ended");
        if(!mouseSwiped)
        {
            NSInteger index = [[[AnswerDataHandler getAnswerDataHandler]workAreaArray] indexOfObject:self];
            [[ImageStoreHelper getImageStoreHelper] saveImageForQuestionNumber:index image:drawImage.image];
        }

    }


 }
SAMIR RATHOD
  • 3,512
  • 1
  • 20
  • 45
Manikandan
  • 107
  • 2
  • 12
  • see my answer. it may help you http://stackoverflow.com/questions/16417841/how-to-make-a-drawing-program/16418120#16418120 – SAMIR RATHOD May 08 '13 at 07:46
  • Hi Samir, Thanks for your answers. But i need to find what is wrong with my code. – Manikandan May 08 '13 at 09:20
  • As far as i can see you only have freehand rounded drawing (using quad curves) and freehand normal drawing using lots of segments (one / touchesMoved ). Am i missing something? – Mihai Timar Oct 13 '13 at 14:48

0 Answers0