CGBITMAPCONTEXTCREATE,UNDO的工作状况不佳



我已经为撤消功能而努力了几天,我真的不知道解决这个问题。

我正在使用使用自定义UIImageView的UiviewController上的绘图函数。

问题是当我触发撤消功能时,上一个绘制的效果很好,但是当我尝试再次绘制时,删除的绘图再次出现在当前绘图中。

这是我的代码。如果您看到任何问题,请告诉我!这真的很有帮助。谢谢!

- (IBAction)Pan:(UIPanGestrueRecognizer *)pan {
    CGContextRef ctxt = [self drawingContext];
    CGContextBeginPath(ctxt);
    CGContextMoveToPoint(ctxt, previous.x, previous.y);
    CGContextAddLineToPoint(ctxt, current.x, current.y);
    CGContextStrokePath(ctxt);
    CGImageRef img = CGBitmapContextCreateImage(ctxt);
    self.costomImageView.image = [UIImage imageWithCGImage:img];
    CGImageRelease(img);
    previous = current;
    ...
}
- (CGContextRef)drawingContext {
    if(!context) { // context is an instance variable of type CGContextRef
        CGImageRef image = self.customImageView.image.CGImage;
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        if(!colorSpace) return nil;
        context = CGBitmapContextCreate(NULL,
                                        CGImageGetWidth(image), CGImageGetHeight(image),
                                        8, CGImageGetWidth(image) * 4,
                                        colorSpace, kCGImageAlphaPremultipliedLast
                                        );
        CGColorSpaceRelease(colorSpace);
        if(!context) return nil;
        CGContextConcatCTM(context, CGAffineTransformMake(1,0,0,-1,0,self.customeImageView.image.size.height));
        CGContextSaveGState(context);
        CGContextTranslateCTM(context, 0.0, self.customImageView.image.size.height);
        CGContextScaleCTM(context, 1.0, -1.0);
        CGContextDrawImage(context, (CGRect){CGPointZero,self.customImageView.image.size}, image);
        CGContextRestoreGState(context);
        CGContextSetLineCap(context, kCGLineCapRound);
        CGContextSetLineWidth(context, 4.f);
        CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
    }
    return context;
}

我不确定您要做什么是由UndoManager

支持

我个人要做的就是这样:

  1. 创建一个NSMutableArray以包含所有图纸笔触。其中有两个Point实例的结构应该为此任务做。如果您想幻想,您可以添加一个UIColor实例来设置颜色和更多变量,以获取其他样式信息。

  2. 当您的锅手势开始时,记录其开始的点。结束时,记录该点,创建结构的实例并将其添加到数组中。

  3. 具有一个渲染过程,可以清除图形上下文,通过数组迭代并将其拉到屏幕。

对于撤消功能,您需要做的就是从数组和重新渲染中删除最后一个条目。

最新更新