Eraser does not work in iOS drawing

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); // [super drawRect:rect]; } break; case ERASE: { [m_curImage drawAtPoint:CGPointMake(0, 0)]; CGContextRef context = UIGraphicsGetCurrentContext(); CGContextClearRect(context, rect); CGContextSetBlendMode(context, kCGBlendModeClear); [super drawRect:rect]; break; } case UNDO: { [m_curImage drawInRect:self.bounds]; break; } case REDO: { [m_curImage drawInRect:self.bounds]; break; } default: break; } } 

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

+3
ios cocoa-touch core-graphics quartz-2d drawing
Jul 16 '12 at 10:17
source share
2 answers

I solved this problem, the problem below is code that works fine

 case :ERASE { CGPoint mid1 = midPoint(m_previousPoint1, m_previousPoint2); CGPoint mid2 = midPoint(m_currentPoint, m_previousPoint1); [m_curImage drawAtPoint:CGPointMake(0, 0)]; 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); CGContextSetBlendMode(context, kCGBlendModeClear); CGContextSetLineJoin(context, kCGLineJoinRound); CGContextSetLineWidth(context, self.lineWidth); CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor); CGContextSetShouldAntialias(context, YES); CGContextSetAllowsAntialiasing(context, YES); CGContextSetFlatness(context, 0.1f); CGContextSetAlpha(context, self.lineAlpha); CGContextStrokePath(context); } 

Ranjit Relations.

This solved my problem, if anyone has the same problem, do this.

+4
Jul 23 '12 at 11:01
source share
— -

Here, what happens when you delete the entire rectangle will get a clear na ?? therefore, instead of using this erasure method, draw a line in the same color as on the screen so that it looks like a window is being cleaned, and it can also be easily tried.

For example, if your drawing screen is black, then draw a line for hatching on the screen in black so that it looks like a cleanup.

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ lastPoint = [touch locationInView:imagevw]; } -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event UITouch *touch = [touches anyObject]; CGPoint currentPoint = [touch locationInView:imagevw]; UIGraphicsBeginImageContext(self.imagevw.frame.size); [self.imagevw.image drawInRect:CGRectMake(0, 0, self.imagevw.frame.size.width, self.imagevw.frame.size.height)]; CGContextSetLineWidth(UIGraphicsGetCurrentContext(), width); CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0); // black color CGContextBeginPath(UIGraphicsGetCurrentContext()); CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); CGContextStrokePath(UIGraphicsGetCurrentContext()); imagevw.image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); 

This is the code for drawing a black line on a black background ... just try ... it can help you ..

+2
Jul 18 '12 at 7:44
source share



All Articles