Objective-C如何避免在具有UIBezierPath笔触颜色的同色图像上绘制 [英] Objective-C How to avoid drawing on same color image having stroke color of UIBezierPath
问题描述
我们正在使用 UIBezierPath
,
我们使用贝塞尔曲线绘制笔划
We did paint strokes using bezier path,
具有蓝色RGB值和0.5 Alpha的透明笔触
with blue color RGB values and 0.5 Alpha for transparent stroke
使用路径数组..我们几乎完成了该应用程序
using paths array.. we almost complete the application,
但是从PathsArray绘制笔画时,仍然存在一些性能问题,这些笔画已经绘制了Bezierpaths,例如在绘制一定数量的笔画后,画笔画变得很慢,
But there are some performance issues to drayong paint strokes from PathsArray which is having alredy drawn Bezierpaths, like paint strokes getting slow after drawing some number of strokes,
因此为避免此性能问题,我们在sketchImage视图后面使用了临时图像视图,
我们在顶部草图图像"视图上绘制了最近的笔触,并使用PathsArray更新了底部的临时图像"视图,
We drawn recent one stroke on Top Sketch Image view and updated the bottom Temperary iamge view with the PathsArray,
它可以正常工作并提高性能,但是存在问题
,由于所有笔划均保存为图像,并且我们尝试绘制另一幅图像,因此笔划会加倍并增加重叠点的不透明度
that since all the strokes saved as image and we try to draw another image the strokes gets double and increase the opacity at overlaping points
实际上我们可以避免使用 kCGBlendModeCopy
但是由于我们使用 kCGBlendModeCopy
那么有什么方法可以避免在相同的涂料上产生划痕
So is there any way to avoid stroke on same paint
推荐答案
您可以通过具有两个图像视图(一个是风景照),最重要的是我们要在其上的另一个图像视图来实现所需的效果.画.但是,与其绘制 alpha
为0.5的图形,不如绘制 alpha
为<1.0的 UIBezierPath
,而是在其自己的 alpha
是0.5.
You can achieve the desired effect by having two image views, the landscape photo in one, and on top of that, another image view upon which we're going to draw. But, rather than drawing with an alpha
of 0.5, draw a UIBezierPath
with alpha
of 1.0, but do it on an image view whose own alpha
is 0.5.
- (void)handlePan:(UIPanGestureRecognizer *)gesture
{
static UIBezierPath *bezierPath = nil;
static CAShapeLayer *shapeLayer = nil;
if (gesture.state == UIGestureRecognizerStateBegan) {
bezierPath = [UIBezierPath bezierPath];
[bezierPath moveToPoint:[gesture locationInView:gesture.view]];
shapeLayer = [CAShapeLayer layer];
shapeLayer.strokeColor = [[UIColor blueColor] CGColor];
shapeLayer.lineWidth = 40.0;
shapeLayer.fillColor = [[UIColor clearColor] CGColor];
shapeLayer.lineCap = kCALineCapRound;
shapeLayer.lineJoin = kCALineJoinRound;
[self.drawImageView.layer addSublayer:shapeLayer];
} else if (gesture.state == UIGestureRecognizerStateChanged) {
[bezierPath addLineToPoint:[gesture locationInView:gesture.view]];
shapeLayer.path = [bezierPath CGPath];
} else if (gesture.state == UIGestureRecognizerStateEnded) {
CGFloat saveAlpha = self.drawImageView.alpha; // save current alpha for future reference
self.drawImageView.alpha = 1.0; // bring alpha back to 1.0 for the purposes of saving image
// grab image
UIGraphicsBeginImageContextWithOptions(self.drawImageView.bounds.size, NO, 0);
if ([self.drawImageView respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])
[self.drawImageView drawViewHierarchyInRect:self.drawImageView.bounds afterScreenUpdates:YES]; // iOS7+
else
[self.drawImageView.layer renderInContext:UIGraphicsGetCurrentContext()]; // pre iOS7
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.drawImageView.image = image; // use new image
self.drawImageView.alpha = saveAlpha; // reduce alpha back to what it was
[shapeLayer removeFromSuperlayer]; // get rid of shape layer
shapeLayer = nil;
bezierPath = nil;
}
}
这篇关于Objective-C如何避免在具有UIBezierPath笔触颜色的同色图像上绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!