http://www.raywenderlich.com/18840/how-to-make-a-simple-drawing-app-with-uikit
他的擦除功能是使用白色绘图.但我正在使用图像背景,当我擦除时,我真的需要擦除.
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,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,size.width,size.height)]; CGContextSetLineCap(UIGraphicsGetCurrentContext(),kCGLineCapRound); CGContextSetLineWidth(UIGraphicsGetCurrentContext(),_width); CGContextSetRGBstrokeColor(UIGraphicsGetCurrentContext(),_alpha); CGContextMoveToPoint(UIGraphicsGetCurrentContext(),_lastPoint.y); CGContextAddLineToPoint(UIGraphicsGetCurrentContext(),_lastPoint.y); CGContextstrokePath(UIGraphicsGetCurrentContext()); CGContextFlush(UIGraphicsGetCurrentContext()); self.tempImageView.image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); } UIGraphicsBeginImageContext(self.drawImageView.frame.sizE); [self.drawImageView.image drawInRect:CGRectMake(0,size.height) blendMode:kCGBlendModeNormal alpha:1.0]; if (_isErasing) { CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeClear); } [self.tempImageView.image drawInRect:CGRectMake(0,self.tempImageView.frame.size.height) blendMode:kCGBlendModeNormal alpha:_alpha]; self.drawImageView.image = UIGraphicsGetImageFromCurrentImageContext(); self.tempImageView.image = nil; UIGraphicsEndImageContext(); }
- (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,_width ); if (_isErasing) { CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeClear); } else { CGContextSetRGBstrokeColor(UIGraphicsGetCurrentContext(),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,_width); if (_isErasing) { CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeClear); } else { CGContextSetRGBstrokeColor(UIGraphicsGetCurrentContext(),_alpha); CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal); } CGContextMoveToPoint(UIGraphicsGetCurrentContext(),size.height) blendMode:kCGBlendModeNormal alpha:1.0]; [self.tempImageView.image drawInRect:CGRectMake(0,size.height) blendMode:kCGBlendModeNormal alpha:_alpha]; self.drawImageView.image = UIGraphicsGetImageFromCurrentImageContext(); self.tempImageView.image = nil; UIGraphicsEndImageContext(); }
以上是大佬教程为你收集整理的iOS核心图形如何擦除绘图全部内容,希望文章能够帮你解决iOS核心图形如何擦除绘图所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。