橡皮擦不适用于iOS绘图

我正在绘制一个项目,我有一个橡皮擦选项。 下面给出的代码是当我启动我的应用程序,并绘制一些线和继续使用橡皮擦。 它工作正常,我得到橡皮擦效果。 现在第二个场景是我画了10条线,然后点击“撤消button”并撤销整个事情,然后我重做整个事情,现在当我点击“橡皮擦button”,并试图抹去一些部分,而是将清除整个绘图。 这是我想弄明白,但我不明白我哪里错了,所以朋友,请帮助我。

以下是我的代码。

- (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; } } 

这些是当我点击撤销/重做时运行的function。

 -(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; } 

请告诉我,我是否做得对。

问候,

兰吉特

我已经解决了这个问题,下面的问题是完美的代码

 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。

这解决了我的问题,如果有人有同样的问题,请执行此。

这里发生了什么事情是当你抹去整个矩形将得到明确呐? 所以不要用这种擦除方法画出与屏幕颜色相同的线条,这样看起来就像窗口正在清除,而且它也很容易尝试这一点。

例如,如果您的绘图屏幕是黑色的,那么在黑色的屏幕上绘制触摸移动的线条,使其看起来像清除。

 -(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(); 

这是在黑色的视图上画黑线的代码…只是试试…它可能会帮助你..