J'ai une application de dessin dans laquelle je voudrais créer une méthode d'annulation. Le dessin a lieu à l'intérieur de la méthode TouchesMoved:Enregistrement de CGContextRef
Je suis en train de créer un CGContextRef et de le pousser dans la pile OU de l'enregistrer dans une propriété de contexte qui peut être restaurée plus tard mais qui n'a pas de chance. Tout conseil serait bon. Voici ce que j'ai ...
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();
}
J'ai une méthode déclenchée par mon bouton Annuler ...
- (IBAction)restoreUndoImage {
UIGraphicsBeginImageContext(self.view.frame.size);
UIGraphicsPopContext();
drawingSurface.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
Quand je cours, je crois que mon DrawingSurface est attribuée nul parce qu'il efface tout dans l'image. Je pense que je ne peux pas utiliser pop et pousser de cette façon. Mais je n'arrive pas à comprendre comment sauvegarder le contexte et le repousser sur la surface de dessin. Hmmmm. Toute aide serait ... eh bien ... utile. Merci d'avance -
Et, juste pour référence, voici ce que je fais pour dessiner à l'écran, qui fonctionne très bien. C'est dans mon 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();
Une réponse fantastique et réfléchie. Je ne sais pas pourquoi OP n'a pas accepté cela. – Tim