2

I am new to iOS and building a paint application.

Everything goes ok..but I am unable to implement NSUndoManager in my application for undo and redo purpose.

The problem is that I am unable to store the previous state of painting for undo.

Here is the code

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

mouseSwiped = NO;
UITouch *touch = [touches anyObject];
lastPoint = [touch locationInView:self.tempDrawImage];
[[self.undoManage prepareWithInvocationTarget:self] 
touchesMoved:touches withEvent:event];
}

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

mouseSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.tempDrawImage];

UIGraphicsBeginImageContext(self.view.frame.size);
[self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, 
self.view.frame.size.height)];
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush );
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
CGContextStrokePath(UIGraphicsGetCurrentContext());
self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext();
[self.tempDrawImage setAlpha:opacity];
UIGraphicsEndImageContext();
lastPoint = currentPoint;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

UIGraphicsBeginImageContext(self.tempDrawImage.frame.size);
[self.tempDrawImage.image drawInRect:
CGRectMake(0, 0, self.view.frame.size.width,   self.view.frame.size.height) 
blendMode:kCGBlendModeNormal alpha:1.0];
self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext();
[[self.undoManage prepareWithInvocationTarget:self] touchesMoved:touches 
withEvent:event];
UIGraphicsEndImageContext();
}
-(void)undo
{
[self.undoManage undo];
}
-(void)redo
{
[self.undoManage redo];
}
Ahmed Salman Tahir
  • 1,783
  • 1
  • 17
  • 26
Santosh
  • 1,254
  • 2
  • 16
  • 31
  • 1
    You're doing it wrong. The data you should be recording during the touches actions should be the *brushstrokes* of the user (with associated attributes like brush colour, brush width, etc.). Then in the `[UIView drawRect:]` subclass you apply these brushstrokes to create the "picture". – trojanfoe Jan 27 '14 at 11:10
  • And to actually address your question; the undo/redo applies to these brushstrokes you have recorded. – trojanfoe Jan 27 '14 at 11:11
  • How to do that... means passing brush colour, brush width etc in NSUndoManager. – Santosh Jan 27 '14 at 11:27
  • 1
    No, you need to create a custom "model" object, that records the stroke and attributes, and you can probably store these models in a mutable array. – trojanfoe Jan 27 '14 at 11:28

0 Answers0