如何在alpha 2中绘制alpha <1的线条

毫无疑问,这可能是一个重复的问题,但我无法从这里的任何职位得到适当的解决办法。 所以我张贴这个新的职位,希望我得到一些解决scheme。

我的绘图应用程序,我想给不透明的线条绘制function。 但是当我把颜色赋值为alpha时,我会在这行之间得到重叠点。

有些地方,我发现在两个视图中绘制解决了这个问题,但我无法理解这背后的逻辑。

在这里输入图像说明

我正在使用普通的石英二维码来画线触摸事件。

CoreGraphics中的重复绘图,我怎么能保持重叠的点比其余的线更黑暗?

诀窍是在自己的缓冲区中画笔描边,在那里你可以正确地剪切alpha,然后把整个事物和背景混合在一起。

以下是一种方法:创build2个视图,一个用于背景,另一个用于行。 在顶视图中绘制一个1的alpha值! 然后将整个前景视图的alpha设置为0.5(或者任何你想使用的值)。

[topView setAlpha:0.5]; 

这将防止半透明的画笔中风加剧。 但是,两个不同的笔触互相交叉(如你的例子)。 你想要那个十字路口更激烈吗? 如果是这样,那么你需要为每个笔触创build一个新的视图。 为了避免内存溢出导致视图太多,您需要将先前的顶视图与背景混合。

枝..

这是我的最终解决scheme..我现在绘制drawRect事件的线条,并一次绘制整条线作为一个path..这创造了不透明性,保留,现在没有线之间的画线..

毫无疑问,这是一个正在工作,因为我在每一个触摸和这个作品的工作..

 - (void)drawRect:(CGRect)rect { for (int j=0; j<[pathArray count]; j++) { context = UIGraphicsGetCurrentContext(); NSMutableDictionary *tmp=[pathArray objectAtIndex:j]; NSMutableArray *currentPath=[tmp objectForKey:@"path"]; for (int i=0; i<[currentPath count]; i++) { CGPoint mid1 = [[self midPoint:[currentPath objectAtIndex:i+1] :[currentPath objectAtIndex:i]] CGPointValue]; CGPoint mid2 = [[self midPoint:[currentPath objectAtIndex:i+2] :[currentPath objectAtIndex:i+1]] CGPointValue]; CGContextMoveToPoint(context, mid1.x, mid1.y); CGContextAddQuadCurveToPoint(context, [[currentPath objectAtIndex:i+1] CGPointValue].x, [[currentPath objectAtIndex:i+1] CGPointValue].y, mid2.x, mid2.y); CGContextSetShadow(context, CGSizeMake(-2, -2), 3); CGContextSetAlpha(context, [[tmp objectForKey: @"opacity"] floatValue]); CGContextSetLineCap(context, kCGLineCapRound); CGContextSetStrokeColorWithColor(context,[[tmp objectForKey: @"Color"] CGColor]); CGContextSetLineWidth(context, [[tmp objectForKey: @"width"] floatValue]); i+=2; } CGContextStrokePath(context); } } 

看起来你正在一系列触摸事件的每一个点上画一个圆圈。 相反,您可以使用CoreGraphics/Quartz2D构build一个Path ,将线宽设置为任意厚度,然后根据需要对该path进行笔触,以保持界面的美观。 我一段时间没有这样做,但是我认为大部分你需要的是CoreGraphics/CGContext.h~/CGPath.h, etc.看到我对这个CoreGraphics问题的回答。

我现在想起的一个未知的是,在使用CGPathCloseSubpath()closures它之前,是否可以将CGMutablePathRef描述为上下文。 你必须尝试。 无论如何,当您收到鼠标事件时,您将build立一个新的点,并一次渲染一点。 让我知道你是否需要澄清。

PS作为opacity ,你会为你的上下文创buildCGColorRef设置它… CoreGraphics/CGColor.h中的许多调用有一个alpha参数。

 #pragma mark - #pragma mark Touch Event - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; lastPoint = [touch locationInView:drawView]; } - (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint currentPoint = [touch locationInView:drawView]; UIGraphicsBeginImageContext(drawView.frame.size); [drawImage.image drawInRect:CGRectMake(0, 0, drawView.frame.size.width, drawView.frame.size.height)]; CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 1.0); CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0); CGContextBeginPath(UIGraphicsGetCurrentContext()); CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); CGContextStrokePath(UIGraphicsGetCurrentContext()); drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); lastPoint = currentPoint; } - (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { UIGraphicsBeginImageContext(drawView.frame.size); [drawImage.image drawInRect:CGRectMake(0, 0, drawView.frame.size.width, drawView.frame.size.height)]; CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 1.0); CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0); CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); CGContextStrokePath(UIGraphicsGetCurrentContext()); CGContextFlush(UIGraphicsGetCurrentContext()); drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); }