Tengo una aplicación de dibujo en la que me gustaría crear un método de deshacer. El sorteo tiene lugar dentro del método TouchesMoved:Guardando CGContextRef
Estoy tratando de crear un CGContextRef y llevarlo a la pila O guardarlo en una propiedad de contexto que se puede restaurar más tarde, pero no estoy teniendo suerte. Cualquier consejo sería genial. Aquí es lo que tengo ...
UIImageView *drawingSurface;
CGContextRef undoContext;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UIGraphicsBeginImageContext(self.view.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[drawingSurface.image drawInRect:CGRectMake(0, 0, drawingSurface.image.size.width, drawingSurface.image.size.height)];
UIGraphicsPushContext(context);
// also tried but cant figure how to restore it
undoContext = context;
UIGraphicsEndImageContext();
}
Luego he un método provocada por mi botón de deshacer ...
- (IBAction)restoreUndoImage {
UIGraphicsBeginImageContext(self.view.frame.size);
UIGraphicsPopContext();
drawingSurface.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
Cuando ejecuto esto, creo que mi drawingSurface se está asignando nula porque simplemente borra todo en la imagen.
Supongo que no puedo usar pop y presionar de esta manera. Pero parece que no puedo imaginar cómo guardar el contexto y luego volver a presionarlo en la superficie de dibujo. Hmmmm Cualquier ayuda sería ... bueno ... útil. Gracias de antemano -
Y, solo como referencia, esto es lo que estoy haciendo para dibujar en la pantalla, que está funcionando muy bien. Esto está dentro de mis TouchesMoved:
UIGraphicsBeginImageContext(self.view.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[drawingSurface.image drawInRect:CGRectMake(0, 0, drawingSurface.image.size.width, drawingSurface.image.size.height)];
CGContextSetLineCap(context, kCGLineCapRound); //kCGLineCapSquare, kCGLineCapButt, kCGLineCapRound
CGContextSetLineWidth(context, self.brush.size); // for size
CGContextSetStrokeColorWithColor (context,[currentColor CGColor]);
CGContextBeginPath(context);
CGContextMoveToPoint(context, lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(context, currentPoint.x, currentPoint.y);
CGContextStrokePath(context);
drawingSurface.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Respuesta fantástica y bien pensada. No estoy seguro de por qué OP no aceptó esto. – Tim