2013-05-09 14 views
7

私は、色で描かれたいくつかの線を描画し、次にそれらの内部(ポリゴンを作る)を別のもので塗りつぶすのに問題があります。コメントアウトCGContextStrokePathストローク用に1色、塗りつぶし用に1色のポリゴンを描画しますか?

UIColor *houseBorderColor = [UIColor colorWithRed:(170/255.0) green:(138/255.0) blue:(99/255.0) alpha:1]; 
CGContextSetStrokeColorWithColor(context, houseBorderColor.CGColor); 
CGContextSetLineWidth(context, 3); 

// Draw the polygon 
CGContextMoveToPoint(context, 20, viewHeight-19.5); 
CGContextAddLineToPoint(context, 200, viewHeight-19.5); // base 
CGContextAddLineToPoint(context, 300, viewHeight-119.5); // right border 
CGContextAddLineToPoint(context, 120, viewHeight-119.5); 
CGContextAddLineToPoint(context, 20, viewHeight-19.5); 

// Fill it 
CGContextSetRGBFillColor(context, (248/255.0), (222/255.0), (173/255.0), 1); 
//CGContextFillPath(context); 

// Stroke it 
CGContextStrokePath(context); 

、私はこの結果を得る:

enter image description here

をしかし、私はCGContextStrokePathのコメントを解除し、ポリゴンを記入した場合、色はストロークをオーバーフロー:

enter image description here

どのようにしてこのような結果が得られますか(whをやり直す必要はありません(それはそれの作業が行われることを期待)

enter image description here

答えて

16

あなただけの最初の操作が成功したような問題は、CGContextFillPath()CGContextStrokePath(context) 両方が明確という現在のパスである代わりに

CGContextFillPath(context); 
CGContextStrokePath(context); 

CGContextDrawPath(context, kCGPathFillStroke); 

を使用することができます第2の 操作は何も描画しません。 CGContextDrawPath()は、間にパスをクリアする なしで塗りとストロークを結合します。あなたがこれを行うことができUIBezierPathを使用して

+0

「CGContextDrawPath()」はどこに行かなくてはいけないのか分かりませんが、私は厳しい要求はしたくありませんが、完全な答えを出すために精巧にできますか? –

+3

申し訳ありませんが、私は電話のために答えがとても短かったです:-)その呼び出しは、CGContextFillPathと... StrokePathの両方を置き換えます。 –

+0

これはこの場合の最適な解決策です。 – alastair

2

あなた脳卒中やコンテキストでパスを記入し、コンテキストがあなたのためのパスを削除する場合:OLE二回描画手順)。ストローク後に塗りつぶしたい場合は、パスを再度追加する必要があります。

CGPathRef pathローカル変数を作成し、パスを作成し、追加し、ストロークし、再度追加し、塗りつぶすのが最善の方法です。

CGMutablePathRef path = CGPathCreateMutable(); 
CGPathMoveToPoint(path, nil, 20, viewHeight-19.5); 
CGPathAddLineToPoint(path nil, 200, viewHeight-19.5); // base 
CGPathAddLineToPoint(path nil, 300, viewHeight-119.5); // right border 
CGPathAddLineToPoint(path nil, 120, viewHeight-119.5); 
CGPathAddLineToPoint(path nil, 20, viewHeight-19.5); 

CGContextAddPath(context, path); 
CGContextFillPath(context); 

// possibly modify the path here if you need to 

CGContextAddPath(context, path); 
CGContextStrokePath(context); 
+0

あなたは、私が提供されたコードを使用して 'PATH'の作成について詳しく説明できますか? –

+1

[CGContext](https://)ではなく[CGPath API](https://developer.apple.com/library/mac/#documentation/graphicsimaging/Reference/CGPath/Reference/reference.html)を使用します。 developer.apple.com/library/mac/#documentation/graphicsimaging/Reference/CGContext/Reference/reference.html)を使用してパスを作成します。しかし、この例では 'CGContextDrawPath()'を使うべきでしょう。なぜなら、実際には 'CGContextFillPath()'によってパスがコンテキストから削除されているからです(空のパスを描いているので、それで何も起こらない)。 – alastair

+0

このコンセプトを説明する時間を割いていただき、本当にありがとうございます。これにより、CGContextに関するすべてのことがはっきりと分かります。ありがとう! –

5

UIBezierPath *path = [[UIBezierPath alloc] init]; 
[path moveToPoint:CGPointMake(20, viewHeight-19.5)]; 
[path addLineToPoint:CGPointMake(200, viewHeight-19.5)]; 
[path addLineToPoint:CGPointMake(300, viewHeight-119.5)]; 
[path addLineToPoint:CGPointMake(120, viewHeight-119.5)]; 
[path addLineToPoint:CGPointMake(20, viewHeight-19.5)]; 

[[UIColor colorWithRed:(248/255.0) green:(222/255.0) blue:(173/255.0) alpha:1.0] setFill]; 
[path fill]; 
[[UIColor colorWithRed:(170/255.0) green:(138/255.0) blue:(99/255.0) alpha:1.0] setStroke]; 
[path stroke]; 
+0

私はUIBezierPathについてAppleの例だけを知っていますが、私の質問では 'CGContext'メソッドとは対照的に利点がありますか? @MikePollard –

+2

私は、Core FoundationオブジェクトではなくNSObjectsを使用した場合の通常のメリットと考えています。 –

関連する問題