-3

Actually i got few samples for drawing image through free hand and i integrated as well in my application.I need additional functionalities such as undo/redo.How can i achieve this?Need help on this..I used following code.

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    mouseSwiped = YES;

    UITouch *touch = [touches anyObject];   
    CGPoint currentPoint = [touch locationInView:self.view];
    currentPoint.y -= 20;


    UIGraphicsBeginImageContext(self.view.frame.size);
    [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    lastPoint = currentPoint;

    mouseMoved++;

    if (mouseMoved == 10) {
        mouseMoved = 0;
    }
}
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
vinay
  • 1,276
  • 3
  • 20
  • 34
  • possible duplicate of http://stackoverflow.com/questions/9528062/how-to-undo-when-we-draw-in-iphone-sdk/9528103#9528103 – Vignesh Mar 08 '12 at 07:25

2 Answers2

5
    CGMutablePathRef path;
    path = CGPathCreateMutable();
    - (void)drawRect:(CGRect)rect
    {
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGPathMoveToPoint(path, NULL, previousPoint.x, previousPoint.y);
    CGPathAddLineToPoint(path, NULL, lastPoint.x, lastPoint.y);
    CGContextAddPath(context, path);

    CGContextSetLineWidth(context, 10.0);
[[UIColor greenColor] setStroke];
    CGContextDrawPath(context,kCGPathFillStroke);
    }

Now you can perform Undo/Redo by path's index.

Veera Raj
  • 1,562
  • 1
  • 19
  • 40
2

You can try storing all paths in an array. When you draw a new one, add it last and draw it, when you delete - remove the last and redraw all. Keep removed in another array so you can redo actions, clear the redo array when a new path is added to the paths array. That's the most straight-forward method I can think of.

Alexander
  • 8,117
  • 1
  • 35
  • 46