我有一个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.
更多推荐
发布评论