2011-08-10 7 views
1

私は奇妙な形のポリゴンのためにCGPathRefを持っています。このパスのの外側にのアルファを適用する必要があります。これはかなり簡単です。2つのCGPathオブジェクトの交差の外側にアルファを適用する

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); 

2つの図形を交差しようとすると問題が発生します。私は両方の図形の内側にないピクセルにアルファを適用したい。

  • 点が円内にあり、ポリゴン内にない場合は、アルファを適用します。
  • ポリゴンにありますがサークル内にない場合は、アルファを適用します。
  • 多角形と円の両方にある場合は、ピクセルは完全に透明でなければなりません。

私はさまざまなアプローチを試みました。誰も働いていない。最も有望なのは、ポリゴンでマスクを作成し、円の描画をバインドするのにCGContextClipToMaskを使用することでした。フル・サークルはクリッピングなしで描かれました。

答えて

0

さらに数時間の試行錯誤の末、私はついにそれを理解しました。

// 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、ない0.0, 0.0あります。ドキュメントではこれを非常に明確にしていますが、シェイプを配置するタイミングを把握しなければならないのは面倒です。

関連する問題