2017-06-11 11 views
1

私は画像で色を塗りつぶし、UIBezierPathを使用してコーナーのポイントを消去する機能があります。ベジエパスの外側を外します

CGRect rect = CGRectMake(0.0f, 0.0f, width, height); 
UIGraphicsBeginImageContext(rect.size); 
CGContextRef context = UIGraphicsGetCurrentContext(); 

CGContextSetBlendMode(context, kCGBlendModeCopy); 

// Fill image 
CGContextSetFillColorWithColor(context, [[UIColor redColor] CGColor]); 
CGContextFillRect(context, rect); 

// Round corners 
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:15.0]; 
CGContextSetStrokeColorWithColor(context, [[UIColor clearColor] CGColor]); 
[bezierPath stroke]; 

UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

上記を使用すると、ベジエのパスが切り取られ、背景が塗りつぶされたイメージが得られます。 Icon with Bézier path cut out

しかし、パスの外にあるコーナーを削除するにはどうすればよいですか、またはどこにあるかを参照してクリアすることができますか?

+0

パスを描画するのではなく、イメージをそのパスにマスクする必要があります。 – Rob

+0

ところで、実際には丸みを帯びたコーナーのイメージを作成する必要があります。なぜなら、UIの丸みのあるコーナーだけを望むならば、ビューの 'layer'の' cornerRadius'を設定することができるからです。 – Rob

+0

@Rob悲しいことに、私はこれをやっている方法でビュー自体を変更することができないので、イメージを使用する必要があります。ありがとう、私はマスキングをチェックアウトします。 – Spotlight

答えて

2

オプションのカップル:

  1. 使用CoreGraphics、あなたが持っているが、パスにクリップのように:、

    また
    CGRect rect = CGRectMake(0.0f, 0.0f, width, height); 
    
    UIGraphicsBeginImageContextWithOptions(rect.size, false, 0); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetBlendMode(context, kCGBlendModeCopy); 
    
    // Round corners 
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:15.0]; 
    CGContextAddPath(context, bezierPath.CGPath); 
    CGContextClip(context); 
    
    // Fill image 
    CGContextSetFillColorWithColor(context, [[UIColor redColor] CGColor]); 
    CGContextFillRect(context, rect); 
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    
  2. をCoreGraphicsを排除し、ちょうどfillUIBezierPath

    UIGraphicsBeginImageContextWithOptions(rect.size, false, 0); 
    [[UIColor redColor] setFill]; 
    [[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:15.0] fill]; 
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    

これら両方の例では、0(問題のデバイスに表示するために最適化されたスケール)を提供するUIGraphicsBeginImageContextWithOptionsを使用しました。本当に必要ならば、網膜デバイスにレンダリングしたときに明らかに少しピクセル化される1のスケールを供給することができますが、それはあなた次第です。

+0

後者は私のインスタンスで完璧に働いた、ありがとう! – Spotlight

関連する問題