想通过 Quartz 在 ipad 绘画应用程序中添加手动擦除选项
Posted
技术标签:
【中文标题】想通过 Quartz 在 ipad 绘画应用程序中添加手动擦除选项【英文标题】:Want to add manual erasing option in ipad painting application by Quartz 【发布时间】:2010-10-05 13:11:49 【问题描述】:我正在开发一个使用 Quartz 2D for ipad 的绘画应用程序。现在我想添加一个橡皮擦选项,以便用户可以通过触摸手动擦除他绘制的线的一部分。我也有背景图像。有人可以帮忙吗?
【问题讨论】:
到目前为止你实现了什么?您是否遇到了具体问题,或者您只是在寻求一般性建议? How to draw a transparent stroke (or anyway clear part of an image) on the iPhone 的可能重复项 另见Issue with erase or transparent stroke using CoreGraphics framework @g-azam 请不要添加不相关的标签。这个问题与 Swift 无关。也许您正在考虑“ios”? 【参考方案1】:是的,这在我的应用中运行良好 ;-)
首先你在touch开始添加这段代码
UITouch *touch = [touches anyObject];
lastPoint = [touch locationInView:imgview];
UIGraphicsBeginImageContext(imgview.frame.size);
[imgview.image drawInRect:CGRectMake(0, 0, imgview.frame.size.width, imgview.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), lineWidth);
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [UIColor colorWithPatternImage:img].CGColor);
CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), [UIColor clearColor].CGColor);
CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES);
CGContextBeginPath(UIGraphicsGetCurrentContext());
这里img是你的背景图片然后在移动触摸你简单地写下代码是这样的线条笔画代码
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:imgview];
NSLog(@"asdasdas");
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
imgview.image = UIGraphicsGetImageFromCurrentImageContext();
lastPoint = currentPoint;
然后你会看到最终的结果是产生 ;-)
【讨论】:
【参考方案2】:if(erase)
UITouch *touch = [touches anyObject];
CGPoint currentTouch = [touch locationInView:extraImageVw];
CGFloat brushSize;
if (isEraser)
brushSize=25.0;
else
brushSize=25.0;
CGColorRef strokeColor = [UIColor whiteColor].CGColor;
UIGraphicsBeginImageContext(extraImageVw.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[extraImageVw.image drawInRect:CGRectMake(0, 0, extraImageVw.frame.size.width, extraImageVw.frame.size.height)];
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, brushSize);
if (isEraser)
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:fullPathToFile]].CGColor);
else
CGContextSetStrokeColorWithColor(context, strokeColor);
CGContextSetBlendMode(context, kCGBlendModeClear);
CGContextSaveGState(UIGraphicsGetCurrentContext());
CGContextBeginPath(context);
CGContextMoveToPoint(context, Lastpoint.x, Lastpoint.y);
CGContextAddLineToPoint(context, currentTouch.x, currentTouch.y);
CGContextStrokePath(context);
extraImageVw.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Lastpoint = [touch locationInView:extraImageVw];
【讨论】:
以上是关于想通过 Quartz 在 ipad 绘画应用程序中添加手动擦除选项的主要内容,如果未能解决你的问题,请参考以下文章