I am working on a drawing project where I have an eraser option. The code is below when I launch my application and draw some lines, and continue to use the eraser. It works great and I get the eraser effect. Now the second scenario is where I draw about 10 lines, and then I click the Undo button and undo it all, then I redo it all, and now when I click the Eraser button and try to erase part, but instead he will clear the whole drawing. This is what I am trying to understand, but I do not understand where I am going wrong, so friends, please help me.
Below is my code.
- (void)drawRect:(CGRect)rect { case DRAW: { [m_curImage drawAtPoint:CGPointMake(0, 0)]; CGPoint mid1 = midPoint(m_previousPoint1, m_previousPoint2); CGPoint mid2 = midPoint(m_currentPoint, m_previousPoint1); CGContextRef context = UIGraphicsGetCurrentContext(); [self.layer renderInContext:context]; CGContextMoveToPoint(context, mid1.x, mid1.y); CGContextAddQuadCurveToPoint(context, m_previousPoint1.x, m_previousPoint1.y, mid2.x, mid2.y); CGContextSetLineCap(context, kCGLineCapRound); CGContextSetLineWidth(context, self.lineWidth); CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor); CGContextSetAlpha(context, self.lineAlpha); CGContextSetAllowsAntialiasing(context, YES); CGContextStrokePath(context);
These are the functions that are triggered when you press cancel / redo.
-(void)redrawLine { UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0f); [self.layer renderInContext:UIGraphicsGetCurrentContext()]; NSDictionary *lineInfo = [m_lineArray lastObject]; m_curImage = (UIImage*)[lineInfo valueForKey:@"IMAGE"]; UIGraphicsEndImageContext(); [self setNeedsDisplayInRect:self.bounds]; } -(void)undoButtonClicked { if([m_lineArray count] > 0) { NSMutableArray *line = [m_lineArray lastObject]; [m_bufferArray addObject:line]; [m_lineArray removeLastObject]; [self redrawLine]; } m_drawStep = UNDO; } -(void)redoButtonClicked { if([m_bufferArray count] > 0) { NSMutableArray *line = [m_bufferArray lastObject]; [m_lineArray addObject:line]; [m_bufferArray removeLastObject]; [self redrawLine]; } m_drawStep = REDO; }
Please tell me if I am doing the right thing.
Hello,
Rangit
ios cocoa-touch core-graphics quartz-2d drawing
Ranjit Jul 16 '12 at 10:17 2012-07-16 10:17
source share