views:

211

answers:

1

I want to be able to save a CGContext into my NSUndoManager using these methods

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:self.view];
       currentPoint.x = currentPoint.x;
       currentPoint.y = currentPoint.y;
       mouseSwiped = YES;
       UIGraphicsBeginImageContext(self.view.frame.size);
       [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
       CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES);
       CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
       CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brushSizeVal);
       CGContextSetAlpha(UIGraphicsGetCurrentContext(), drawingColorAlpha);
       CGContextSaveGState(UIGraphicsGetCurrentContext());    //Probably right here
       CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 
             drawingColorRed,
             drawingColorGreen,
             drawingColorBlue,
             drawingColorAlpha);
       CGContextBeginPath(UIGraphicsGetCurrentContext());
       CGContextMoveToPoint(UIGraphicsGetCurrentContext(),
             lastPoint.x,
             lastPoint.y);
       CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), 
             currentPoint.x,
             currentPoint.y);
       CGContextStrokePath(UIGraphicsGetCurrentContext());
       drawImage.image = UIGraphicsGetImageFromCurrentImageContext();    //drawImage is the UIImageView that I am drawing my context to
        NSLog(@"current point x: %d current point y: %d",currentPoint.x, currentPoint.y);    //Used for my purposes
       lastPoint = currentPoint;
       mouseMoved++;

and

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{
    if (!optionsDisplayerVisible && canDraw)
     {
     if(!mouseSwiped)
      {
      UIGraphicsBeginImageContext(self.view.frame.size);
      [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
      CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES);
      CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
      CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brushSizeVal);
      CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 
               drawingColorRed,
               drawingColorGreen,
               drawingColorBlue,
               drawingColorAlpha);
      CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
      CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
      CGContextStrokePath(UIGraphicsGetCurrentContext());
      CGContextFlush(UIGraphicsGetCurrentContext());
      drawImage.image = UIGraphicsGetImageFromCurrentImageContext();    //drawImage is the UIImageView that I am drawing to
      UIGraphicsEndImageContext();
      }
     }
}

By the way if you want to know I have my NSUndoManager named undoDrawing

A: 

I don't believe you want to save the CGContext, since it's part of a stack that will not be valid when you need it later. Instead, just save the image itself. First, don't create the context over the whole frame. You just need the rectangle between currentPoint and lastPoint. Then, before you start drawing, grab the image from the current context (UIGraphicsGetImageFromCurrentImageContext ()) and save that off along with the frame to the NSUndoManager.

If you're unfamiliar with optimizing your drawing just to the dirty rectangle, start with the simpler version of this and just save the entire image to the undomanager before drawing on it.

Rob Napier