1

サブクラス化UIViewし、上書き-drawRect:し、で図形を描きますCGContext。そして今、私はこの図をで埋めたいと思いUIImageます。それを行う方法はありますか?

編集:

からの私のコード-drawRect:

CGContextBeginPath(ctx);
//here I draw my figure
CGContextClosePath(ctx);
CGContextClip(ctx);

//this doesn't work
UIImage * image = [UIImage imageNamed:@"blackbackground.jgp"];
[image drawInRect:rect];

//this neither
CGImageRef cgImage = image.CGImage;
CGContextDrawImage(ctx, rect,  cgImage);
4

3 に答える 3

2

に画像を描画する適切な方法 CGContext は次のとおりです。

UIImage *_originalImage = // your image

UIGraphicsBeginImageContext(_originalImage.size);
CGContextRef _context = UIGraphicsGetCurrentContext(); // here you don't need this reference for the context but if you want to use in the future for drawing anything else on the context you could get it for it
[_originalImage drawInRect:CGRectMake(0.f, 0.f, _originalImage.size.width, _originalImage.size.height)];

UIImage *_newImage = UIGraphicsGetImageFromCurrentImageContext();    
UIGraphicsEndImageContext();
于 2012-07-18T08:46:28.300 に答える
0

必要なのは、図のパスでコンテキストをクリップしてから、イメージを描画することです。最初の部分を実行する方法を説明するこの回答を見てくださいUILabel のテキストにグラデーションを追加するにはどうすればよいですか?背景には追加しませんか? コンテキストをクリップしたら、UIImage を取得し、drawInRect などの描画関数の 1 つを呼び出すだけです。

于 2012-07-17T13:42:19.233 に答える
0

Figure の「内側」に画像を描画することを意味すると仮定すると、最も簡単な方法は、Figure の cgpath を作成し、パスが閉じていることを確認してから、CGContextClipToPath( context, path) を実行することです。

追加した

あなたのコードをテストしたところ、Peter が言ったように動作するはずです。パスをテストし (ストロークの色とストロークのパスを設定)、マスクなしで画像を試して問題の場所を特定することをお勧めします。

于 2012-07-17T12:21:07.957 に答える