应用2个CGPath对象的alpha外部交叉点(Applying alpha outside intersection of 2 CGPath objects)

编程入门 行业动态 更新时间:2024-10-12 05:48:47
应用2个CGPath对象的alpha外部交叉点(Applying alpha outside intersection of 2 CGPath objects)

我有一个CGPathRef 。 我需要此路径之外的区域应用alpha。 这非常简单。

CGContextAddPath(context, crazyPolygon); CGContextSetFillColor(context, someAlphaColor); CGContextEOFillPath(context);

我需要用圆圈做类似的事情,也很简单。

CGMutablePathRef circlePath = CGPathCreateMutable(); CGPathAddRect(circlePath, NULL, rect); CGPathAddEllipseInRect(circlePath, NULL, circleBox); CGContextAddPath(context, circlePath); CGContextSetFillColor(context, someAlphaColor); CGContextEOFillPath(context);

当我尝试将两个形状相交时会出现问题。 我想将alpha应用于不在两个形状内的任何像素。

如果该点位于圆中但不在多边形内,则应用alpha。 如果它在多边形中但不在圆圈中,则应用alpha。 如果它在多边形和圆形中,则像素应该是完全透明的。

我尝试了很多不同的方法。 没有人工作过。 最有希望的是用多边形创建一个蒙版,并使用CGContextClipToMask来绑定圆的绘图。 虽然画了整个圆圈但没有剪裁。

I have a CGPathRef for an oddly shaped polygon. I need to apply an alpha to the area outside of this path. This is pretty straightforward.

CGContextAddPath(context, crazyPolygon); CGContextSetFillColor(context, someAlphaColor); CGContextEOFillPath(context);

I need to do something similar with a circle, also straightforward.

CGMutablePathRef circlePath = CGPathCreateMutable(); CGPathAddRect(circlePath, NULL, rect); CGPathAddEllipseInRect(circlePath, NULL, circleBox); CGContextAddPath(context, circlePath); CGContextSetFillColor(context, someAlphaColor); CGContextEOFillPath(context);

The problem arises when I try to intersect the two shapes. I want to apply alpha to any pixel that isn't inside both shapes.

If the point is in the circle but not within the polygon, apply the alpha. If it is in the polygon but not in the circle, apply the alpha. If it is in both the polygon and the circle, the pixel should be fully transparent.

I've tried a bunch of different approaches. None have worked. The most promising was to create a mask with the polygon and using CGContextClipToMask to bound the drawing of the circle. The full circle was drawn with no clipping though.

最满意答案

经过几个小时的反复试验,我终于明白了。

// Set up CGContextRef context = UIGraphicsGetCurrentContext(); CGFloat outOfAreaColor[4] = { 0.0, 0.0, 0.0, OUT_OF_AREA_ALPHA }; CGContextSetFillColor(context, outOfAreaColor); // Path for specifying outside of polygons CGMutablePathRef outline = CGPathCreateMutable(); CGPathAddRect(outline, NULL, rect); CGPathAddPath(outline, NULL, path); // Fill the area outside of the path with an alpha mask CGContextAddPath(context, outline); CGPathRelease(outline); CGContextEOFillPath(context); // Add the inside path to the context and clip the context to that area CGContextAddPath(context, insidePolygon); CGContextClip(context); // Create a path defining the area to draw outside of the circle // but within the polygon CGRect circleBox = CGRectMake(0, 0, circleRadius * 2.0, circleRadius * 2.0); CGMutablePathRef darkLayer = CGPathCreateMutable(); CGPathAddRect(darkLayer, NULL, rect); CGPathAddEllipseInRect(darkLayer, NULL, circleBox); CGContextAddPath(context, darkLayer); CGContextEOFillPath(context); CGPathRelease(darkLayer);

使用CGPathAddEllipseInRect ,圆/椭圆的中心是circleBox.origin.x + circleBox.size.width / 2.0, circleBox.origin.y + circleBox.size.height / 2.0 ,而不是circleBox.origin.x + circleBox.size.width / 2.0, circleBox.origin.y + circleBox.size.height / 2.0 。 文档使这一点非常清楚,但是在定位形状时必须弄明白这一点很烦人。

After a couple hours of more trial and error, I finally figured it out.

// Set up CGContextRef context = UIGraphicsGetCurrentContext(); CGFloat outOfAreaColor[4] = { 0.0, 0.0, 0.0, OUT_OF_AREA_ALPHA }; CGContextSetFillColor(context, outOfAreaColor); // Path for specifying outside of polygons CGMutablePathRef outline = CGPathCreateMutable(); CGPathAddRect(outline, NULL, rect); CGPathAddPath(outline, NULL, path); // Fill the area outside of the path with an alpha mask CGContextAddPath(context, outline); CGPathRelease(outline); CGContextEOFillPath(context); // Add the inside path to the context and clip the context to that area CGContextAddPath(context, insidePolygon); CGContextClip(context); // Create a path defining the area to draw outside of the circle // but within the polygon CGRect circleBox = CGRectMake(0, 0, circleRadius * 2.0, circleRadius * 2.0); CGMutablePathRef darkLayer = CGPathCreateMutable(); CGPathAddRect(darkLayer, NULL, rect); CGPathAddEllipseInRect(darkLayer, NULL, circleBox); CGContextAddPath(context, darkLayer); CGContextEOFillPath(context); CGPathRelease(darkLayer);

When using CGPathAddEllipseInRect, the center of the circle/ellipse is circleBox.origin.x + circleBox.size.width / 2.0, circleBox.origin.y + circleBox.size.height / 2.0, not 0.0, 0.0. The documentation makes this very clear, but it's annoying to have to figure out when positioning your shapes.

更多推荐

本文发布于:2023-08-07 13:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464738.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:交叉点   对象   alpha   CGPath   intersection

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!