【发布时间】:2019-09-22 17:57:46
【问题描述】:
我在撤销功能上苦苦挣扎了几天,我真的不知道如何解决这个问题。
我正在使用自定义 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;
}
【问题讨论】:
标签: ios objective-c image core-graphics