1

iOSで画像の透明部分を別の画像で塗りつぶしたいと思います。

でいくつか試してみUIGraphicsContextましたが、これまでに使用したことがないため、機能しません。

画像は次のとおりです。

ここに画像の説明を入力

私が試したいくつかのコード:

UIImageView *v = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 70, 70)];

UIGraphicsBeginImageContextWithOptions(v.frame.size, YES, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
// beach image
UIImage *img = [UIImage imageNamed:@"zivogosce_camping_05.jpg"];
//[img drawInRect:CGRectMake(2, 0, 12, 12)];

UIImage *annotationImg = [UIImage imageNamed:@"custom_annotation"];
[annotationImg drawAtPoint:CGPointMake(0, 0)];

CGContextSetFillColorWithColor(context, [[UIColor colorWithPatternImage:img] CGColor]);
CGContextFillRect(context, CGRectMake(0, 0, 50, 50));


CGContextDrawImage(context, CGRectMake(0, 0, v.frame.size.width, v.frame.size.height), [annotationImg CGImage]);



UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

[v setImage:newImage];

UIGraphicsEndImageContext();

でも画像がうまくまとまらない…

4

1 に答える 1

1

これが私がそれを行う方法です:

次のようにして、円の形でクリッピング パスを作成できます。

CGContextSaveGState(context);
CGRect circleRect = CGRectMake(circleCenter.x - radius, circleCenter.y - radius, radius * 2.0, radius * 2.0);
CGContextAddEllipseInRect (context, circleRect);
CGContextClip(context);

// ... do whatever you need to in order to draw the inner image ...

CGContextRestoreGState(context);

// ... draw the transparent png ...

クリッピング パスを設定すると、パス内でのみ描画が行われます。状態を復元すると、クリッピング パスが削除されます (または、通常はキャンバス全体に描画できる以前の値に戻されます)。

于 2014-03-25T19:11:01.667 に答える