1

2 つの長方形 (2 つの閉じたサブcgpaths) があります。長方形 B は小さく、長方形 A 内に存在します。その中にあるすべてのエッジ。長方形Bの外側の色領域を直接塗りつぶす方法はありますか.

CAShapeLayer fillExternalColorそんな感じ?直接的な方法ではない場合、プログラムでこれを行う方法は?

A - 紫色 B ​​- 黄色

Aを描いてからBを描く Bを描いてからAを描く

ということで、Aを描いてからBを描いてみました。Bはクリアカラーにしたかったのですが(とりあえずイエローを入れました)、Aが紫色に見えてしまいました。

AnB を与える CGRectIntersection メソッドと AuB を与える CGRectUnion メソッドを見つけました。AuB - AnB である残りの領域を与える方法はありますか?

4

5 に答える 5

1

マスクされた外側部分を持つ UIImage に赤い境界線の内側の長方形を追加したかったのです。

このページにたどり着きました。次のコードは CGContextEOFillPath を使用しており、私のような他の人に役立ちます。(コードの一部は他のページから収集されます。)

    -(UIImage ) imageByDrawingBorderRectOnImage:(UIImage )image theRect:(CGRect)theRect
    {
        // begin a graphics context of sufficient size
        UIGraphicsBeginImageContext(image.size);

        // draw original image into the context
        [image drawAtPoint:CGPointZero];

        // get the context for CoreGraphics
        CGContextRef ctx = UIGraphicsGetCurrentContext();

        // set stroking color and to draw rect
        [[UIColor redColor] setStroke];

        // drawing with a red stroke color
        CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);

        // the line width to 3
        CGContextSetLineWidth(ctx, 3.0);

        // Add Stroke Rectangle,
        CGContextStrokeRect(ctx, theRect);

        // Now draw fill outside part with partial alpha gray color
        // drawing with a gray stroke color
        CGMutablePathRef aPath = CGPathCreateMutable();
        // outer rectangle
        CGRect rectangle = CGRectMake( 0, 0, image.size.width, image.size.height);
        CGPathAddRect(aPath, nil, rectangle);
        // innter rectangle
        CGPathAddRect(aPath, nil, theRect);
        // set gray transparent color
        CGContextSetFillColorWithColor(ctx, [UIColor colorWithRed:0.75 green:0.75 blue:0.75 alpha:0.5].CGColor);
        // add the path to Context
        CGContextAddPath(ctx, aPath);
        // This method uses Even-Odd Method to draw in outer rectangle
        CGContextEOFillPath(ctx);

        // make image out of bitmap context
        UIImage *retImage = UIGraphicsGetImageFromCurrentImageContext();

        // free the context
        UIGraphicsEndImageContext();

        return retImage;
    }

よろしく。

于 2015-07-24T06:33:10.673 に答える