【发布时间】:2012-07-13 07:27:45
【问题描述】:
我正在 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;
}
}
下图是按下撤消按钮时发生的情况
第一张图片是在点击撤销按钮之前,第二张图片是在点击撤销按钮之后
问候 兰吉特
【问题讨论】:
标签: ios cocoa-touch drawing core-graphics quartz-graphics