元に戻すメソッドを作成したい描画アプリがあります。描画は TouchesMoved: メソッド内で行われます。
CGContextRef を作成してスタックにプッシュするか、後で復元できるコンテキスト プロパティに保存しようとしていますが、うまくいきません。どんなアドバイスも素晴らしいでしょう。これが私が持っているものです...
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();
}
次に、元に戻すボタンによってトリガーされるメソッドがあります...
- (IBAction)restoreUndoImage {
UIGraphicsBeginImageContext(self.view.frame.size);
UIGraphicsPopContext();
drawingSurface.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
これを実行すると、画像内のすべてが消去されるため、drawingSurface に nil が割り当てられていると思います。
私の推測では、この方法で pop と push を使用することはできません。しかし、コンテキストを保存してから drawingSurface に戻す方法がわかりません。うーん。どんな助けでも...まあ...役に立ちます。前もって感謝します -
参考までに、画面に描画するために私が行っていることを次に示します。これはうまく機能しています。これは私の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();