2

パスを赤で塗りつぶしたい。これは私のコードです:

CGFloat red[4] = {1.0f, 0.0f, 0.0f, 1.0f};
CGContextBeginPath(c);
CGContextSetStrokeColor(c, red);
CGContextSetFillColorWithColor(c, [UIColor whiteColor].CGColor);
CGContextMoveToPoint(c, 10, 10);
CGContextAddLineToPoint(c, 20, 20);
CGContextAddLineToPoint(c, 20, 40);
CGContextAddLineToPoint(c, 40, 20);
CGContextAddLineToPoint(c, 10, 10);
CGContextStrokePath(c);
CGContextFillPath(c);

CGContextSetFillColor()andを使用する必要があることはわかっていますCGContextFillPath()が、機能していません。

どうすればこれを正しく行うことができますか?

4

3 に答える 3

3

私の場合の答えは次のとおりです。

CGContextRef context = UIGraphicsGetCurrentContext();

//set fill pattern
UIColor *patternRed = [UIColor colorWithPatternImage:[UIImage imageNamed:@"patternRed.png"]];

CGContextSetLineWidth(context, 1.0);
CGContextSetFillColorWithColor(context, patternRed.CGColor);

CGMutablePathRef pathRef = CGPathCreateMutable();

CGPathMoveToPoint(pathRef, NULL, 0, self.frame.size.height);
for (int i = 0; i<255; ++i) {
    CGPathAddLineToPoint(pathRef, NULL, stepX*arrayX[i], self.frame.size.height - (arrayYRed[i]*stepY));
}
CGPathAddLineToPoint(pathRef, NULL, stepX*255, self.frame.size.height);
CGContextAddPath(context, pathRef);
CGContextFillPath(context);

CGPathRelease(pathRef);
于 2012-04-27T19:42:48.703 に答える
0

迅速な使用のために:

CGContextSetFillColorWithColor(context, UIColor.blackColor().CGColor)
CGContextFillPath(context)
于 2015-04-09T15:44:15.163 に答える
0

上記の例では線幅を設定していないため、四角形の左側に線を引く健全な例を次に示します。drawRect 関数から直接取得

CGContextRef context = UIGraphicsGetCurrentContext();
[[UIColor whiteColor] setFill];
CGContextFillRect(context, rect);

CGContextSetLineWidth(context, 2);
CGFloat gray[4] = {0.5f, 0.5f, 0.5f, 1.0f};
CGContextSetStrokeColor(context, gray);

// left
CGContextMoveToPoint(context, 0, 0);
CGContextAddLineToPoint(context, 0, CGRectGetHeight(rect));

CGContextStrokePath(context);
于 2012-04-27T10:54:54.420 に答える