我正在 iOS 中进行绘图,我的代码可以正常工作,绘图工作正常,现在我已经实现了撤消和重做功能,但是发生的事情是,假设我画了两条线,然后按撤消按钮,然后是第一行绘制变得模糊,我不明白为什么会发生,下面是我的撤消代码。
-
(void)redrawLine
{
UIGraphicsBeginImageContext(self.bounds.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
NSDictionary *lineInfo = [lineArray lastObject];
curImage = (UIImage*)[lineInfo valueForKey:@"IMAGE"];
UIGraphicsEndImageContext();
[self setNeedsDisplayInRect:self.bounds];
[self setNeedsDisplay];
}
-(void)undoButtonClicked
{
if([lineArray count]>0){
NSMutableArray *_line=[lineArray lastObject];
[bufferArray addObject:[_line copy]];
[lineArray removeLastObject];
drawStep = UNDO;
[self redrawLine];
}
}
- (void)drawRect:(CGRect)rect
{
switch (drawStep) {
case DRAW:
{
[curImage drawAtPoint:CGPointMake(0, 0)];
CGPoint mid1 = midPoint(previousPoint1, previousPoint2);
CGPoint mid2 = midPoint(currentPoint, previousPoint1);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.layer renderInContext:context];
CGContextMoveToPoint(context, mid1.x, mid1.y);
CGContextAddQuadCurveToPoint(context, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y);
CGContextSetLineCap(context, kCGLineCapButt);
CGContextSetLineWidth(context, self.lineWidth);
CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
CGContextSetAlpha(context, self.lineAlpha);
CGContextSetAllowsAntialiasing(context, YES);
CGContextStrokePath(context);
[super drawRect:rect];
}
case UNDO:
{
[curImage drawInRect:self.bounds];
break;
}
}
下面是按下撤消按钮时发生的情况的图像
第一个图像是在单击撤消按钮之前,第二个图像是在单击撤消按钮之后
问候兰吉特