iOS Core Graphics如何清除绘图

我正在关注我的涂鸦应用程序的教程。

http://www.raywenderlich.com/18840/how-to-make-a-simple-drawing-app-with-uikit

他的擦除function是使用白色的绘图。 但我正在使用图像背景,当我擦除时,我真的需要擦除。

我努力了

CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear); 

但它不工作。 这是我的代码:

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { _mouseSwiped = NO; UITouch *touch = [touches anyObject]; _lastPoint = [touch locationInView:self.tempImageView]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { _mouseSwiped = YES; UITouch *touch = [touches anyObject]; CGPoint currentPoint = [touch locationInView:self.tempImageView]; UIGraphicsBeginImageContext(self.tempImageView.frame.size); [self.tempImageView.image drawInRect:CGRectMake(0, 0, self.tempImageView.frame.size.width, self.tempImageView.frame.size.height)]; CGContextMoveToPoint(UIGraphicsGetCurrentContext(), _lastPoint.x, _lastPoint.y); CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); CGContextSetLineWidth(UIGraphicsGetCurrentContext(), _width ); CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), _red, _green, _blue, 1.0); if (_isErasing) { CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear); } else { CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal); } CGContextStrokePath(UIGraphicsGetCurrentContext()); self.tempImageView.image = UIGraphicsGetImageFromCurrentImageContext(); [self.tempImageView setAlpha:_alpha]; UIGraphicsEndImageContext(); _lastPoint = currentPoint; } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { CGSize size = self.tempImageView.frame.size; if(!_mouseSwiped) { UIGraphicsBeginImageContext(self.tempImageView.frame.size); [self.tempImageView.image drawInRect:CGRectMake(0, 0, size.width, size.height)]; CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); CGContextSetLineWidth(UIGraphicsGetCurrentContext(), _width); CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), _red, _green, _blue, _alpha); CGContextMoveToPoint(UIGraphicsGetCurrentContext(), _lastPoint.x, _lastPoint.y); CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), _lastPoint.x, _lastPoint.y); CGContextStrokePath(UIGraphicsGetCurrentContext()); CGContextFlush(UIGraphicsGetCurrentContext()); self.tempImageView.image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); } UIGraphicsBeginImageContext(self.drawImageView.frame.size); [self.drawImageView.image drawInRect:CGRectMake(0, 0, size.width, size.height) blendMode:kCGBlendModeNormal alpha:1.0]; if (_isErasing) { CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear); } [self.tempImageView.image drawInRect:CGRectMake(0, 0, self.tempImageView.frame.size.width, self.tempImageView.frame.size.height) blendMode:kCGBlendModeNormal alpha:_alpha]; self.drawImageView.image = UIGraphicsGetImageFromCurrentImageContext(); self.tempImageView.image = nil; UIGraphicsEndImageContext(); } 

通过在触摸开始时交换tempImageView和drawImageView来解决这个问题

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { _mouseSwiped = NO; UITouch *touch = [touches anyObject]; _lastPoint = [touch locationInView:self.tempImageView]; if (_isErasing) { self.tempImageView.image = self.drawImageView.image; self.drawImageView.image = nil; } } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { _mouseSwiped = YES; UITouch *touch = [touches anyObject]; CGPoint currentPoint = [touch locationInView:self.tempImageView]; UIGraphicsBeginImageContext(self.tempImageView.frame.size); [self.tempImageView.image drawInRect:CGRectMake(0, 0, self.tempImageView.frame.size.width, self.tempImageView.frame.size.height)]; CGContextMoveToPoint(UIGraphicsGetCurrentContext(), _lastPoint.x, _lastPoint.y); CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); CGContextSetLineWidth(UIGraphicsGetCurrentContext(), _width ); if (_isErasing) { CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear); } else { CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), _red, _green, _blue, 1.0); CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal); } CGContextStrokePath(UIGraphicsGetCurrentContext()); self.tempImageView.image = UIGraphicsGetImageFromCurrentImageContext(); [self.tempImageView setAlpha:_alpha]; UIGraphicsEndImageContext(); _lastPoint = currentPoint; } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { CGSize size = self.tempImageView.frame.size; if(!_mouseSwiped) { UIGraphicsBeginImageContext(self.tempImageView.frame.size); [self.tempImageView.image drawInRect:CGRectMake(0, 0, size.width, size.height)]; CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); CGContextSetLineWidth(UIGraphicsGetCurrentContext(), _width); if (_isErasing) { CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear); } else { CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), _red, _green, _blue, _alpha); CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal); } CGContextMoveToPoint(UIGraphicsGetCurrentContext(), _lastPoint.x, _lastPoint.y); CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), _lastPoint.x, _lastPoint.y); CGContextStrokePath(UIGraphicsGetCurrentContext()); CGContextFlush(UIGraphicsGetCurrentContext()); self.tempImageView.image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); } UIGraphicsBeginImageContext(self.drawImageView.frame.size); [self.drawImageView.image drawInRect:CGRectMake(0, 0, size.width, size.height) blendMode:kCGBlendModeNormal alpha:1.0]; [self.tempImageView.image drawInRect:CGRectMake(0, 0, size.width, size.height) blendMode:kCGBlendModeNormal alpha:_alpha]; self.drawImageView.image = UIGraphicsGetImageFromCurrentImageContext(); self.tempImageView.image = nil; UIGraphicsEndImageContext(); } 

如果任何人仍然是这样的,它是如何工作在我在Swift 3.感谢您的帮助OMGPOP

 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { swiped = false hideAllButtons() if let touch = touches.first { lastPoint = touch.location(in: self.view) } if isErasing { self.tempImageView.image = self.mainImageView.image self.mainImageView.image = nil } } override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { swiped = true if let touch = touches.first { let currentPoint = touch.location(in: view) drawLineFrom(fromPoint: lastPoint, toPoint: currentPoint) lastPoint = currentPoint } } override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { showAllButtons() if !swiped { drawLineFrom(fromPoint: lastPoint, toPoint: lastPoint) } UIGraphicsBeginImageContext(mainImageView.frame.size) mainImageView.image?.draw(in: CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height), blendMode: .normal, alpha: 1.0) tempImageView.image?.draw(in: CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height), blendMode: .normal, alpha: opacity) mainImageView.image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() tempImageView.image = nil } // Draws a line between two points on the view func drawLineFrom(fromPoint: CGPoint, toPoint: CGPoint) { UIGraphicsBeginImageContext(view.frame.size) let context = UIGraphicsGetCurrentContext() tempImageView.image?.draw(in: CGRect(x:0, y:0, width: view.frame.size.width, height: view.frame.size.height)) context?.move(to: fromPoint) context?.addLine(to: toPoint) context?.setLineCap(.round) context?.setLineWidth(brushWidth) if isErasing { context?.setBlendMode(.clear) } else { context?.setStrokeColor(red: red, green: green, blue: blue, alpha: 1.0) context?.setBlendMode(.normal) } context?.strokePath() tempImageView.image = UIGraphicsGetImageFromCurrentImageContext() tempImageView.alpha = opacity UIGraphicsEndImageContext() } 

你为什么不画一个单独的视图?

TopView = Paint +手势….,BottomView =背景图片。

擦除颜色= ClearColor(透明)

最后,如果你想保存你的作品,由你来组合这两张照片。