Reputation: 1
I know this kind of post was asked a lot of time, but I searched a lot on Google and on this site too, without finding any solution, that's why I'm posting this question. I would like to undo my CGContext line that I draw, without using UIBezierPath or anything else. Is there a way ? Here is the code I use to draw:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:mainImageView];
UIGraphicsBeginImageContext(mainImageView.frame.size);
[mainImageView.image drawInRect:CGRectMake(0, 0, mainImageView.frame.size.width, mainImageView.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), dimension);
const CGFloat *components = CGColorGetComponents([color CGColor]);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), components[0], components[1], components[2], components[3]);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
mainImageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;}
Thanks.
Upvotes: 0
Views: 745
Reputation: 18551
Since you're drawing into an image you can just null out the image and the visible line will be gone.
mainImageView.image = NULL;
// Or
mainImageView.image = originalImage; // Where originalImage is your background if you're using one.
Update
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:mainImageView];
UIGraphicsBeginImageContext(mainImageView.frame.size);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), dimension);
const CGFloat *components = CGColorGetComponents([color CGColor]);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), components[0], components[1], components[2], components[3]);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
// Add the line as a subView.
[mainImageView addSubView:[[UIImageView alloc] initWithImage:UIGraphicsGetImageFromCurrentImageContext()]];
UIGraphicsEndImageContext();
lastPoint = currentPoint;
}
- (void)undoLastLine
{
[mainImageView.subviews.lastObject removeFromSuperview];
}
Upvotes: 0
Reputation: 16022
To add undo:
You can't reveal any info in an image once it's been painted over...
You'll either have to keep copies of the image each time it's changed (lots of memory, although this could be reduced perhaps by keeping just copies of the changed areas only) or, keep a record of the user's drawing steps and then replay them after an undo.
Upvotes: 1