Question

i'm doing an "finger drawing app". I'm using Apple's example [here][1].

But i want to add an undo option. In order to do this, i'm saving the EAGLContext content into an image every time the app handles the end of a touch event.

The problem comes when i want to draw the last image saved in the folowing method:

-(void)undoButtonClicked {
if([savedImagesArray count]>0){
            UIImage *image = [savedImages lastObject];
            [savedImagesArray removeLastObject];
            [image drawInRect:self.frame];
}

}

But the image is not drawn and there is a console output that says:

 <Error>: CGContextRestoreGState: invalid context 0x0

I believe it's something wrong with the context,but i can't seems to figure it out.

Thanks

Was it helpful?

Solution

At the point you're calling it, there is no current context. A new context is generated prior to calling drawRect:, and disposed of at the end of the drawing cycle. You can't call drawInRect: when there is no context.

The view should not be keeping track of old images. That's a controller function. When undo is clicked, the controller should hand the correct image to the view, and the view should then redraw itself the next time drawRect: is called.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top