Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to implement undo in a drawing app
    primarykey
    data
    text
    <p>Below is the code snippet of painting. I can do undo in a vector drawing just like storing points and remove the highest one from mutable array then redrawing.However, it does not function properly in a raster drawing.</p> <p>If I use UIGraphicsGetCurrentContext() as a context reference, undo works well. But the context of CGBitmapContextCreate() does not when issue undo action.</p> <pre><code>- (id)initWithFrame:(CGRect)frame { objArray = [[NSMutableArray alloc] init]; CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); canvas = CGBitmapContextCreate(NULL, drawImage.frame.size.width, drawImage.frame.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast); CGColorSpaceRelease(colorSpace); } return self; } </code></pre> <p>&nbsp;</p> <pre><code>- (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGImageRef imgRef = CGBitmapContextCreateImage(canvas); CGRect r = self.bounds; CGContextDrawImage(context, CGRectMake(0, 0, r.size.width, r.size.height), imgRef); if(ok) { for (int i = 0; i &lt; [objArray count]; i++) { CGPoint point = [[[objArray objectAtIndex: i] objectAtIndex:0] CGPointValue]; CGContextMoveToPoint(canvas, point.x, point.y); for (int j = 0; j &lt; [[objArray objectAtIndex:i] count]; j++) { point = [[[objArray objectAtIndex: i] objectAtIndex:j] CGPointValue]; CGContextAddLineToPoint(canvas, point.x, point.y); CGContextStrokePath(**canvas**); CGContextMoveToPoint(**canvas**, point.x, point.y); } } } CGImageRelease(imgRef); } </code></pre> <p>&nbsp;</p> <pre><code>- (void)undo:(id) sender { NSLog(@"click"); if([objArray count] &gt; 0) [objArray removeLastObject]; ok = YES; [self setNeedsDisplay]; } </code></pre> <p>&nbsp;</p> <pre><code>- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { NSMutableArray *points = [NSMutableArray array]; UITouch *touch = nil; if (touchPoint) { touch = [touches member:touchPoint]; } end = [touch locationInView:self]; [points addObject:[NSValue valueWithCGPoint:start]]; [points addObject:[NSValue valueWithCGPoint:end]]; [objArray addObject:points]; CGContextMoveToPoint(**canvas**, start.x, start.y); CGContextAddLineToPoint(**canvas**, end.x, end.y); CGContextSetLineCap(**canvas**, kCGLineCapRound); CGContextSetLineWidth(**canvas**, 40.0); CGContextStrokePath(**canvas**); start = end; [self setNeedsDisplay]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload