核心图形CGContextSetFillColorWithColor没有填充我的形状

时间:2012-12-27 22:59:39

标签: iphone objective-c ios core-graphics

我有以下形状,我需要填充颜色或背景图像:

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSaveGState(context);

CGMutablePathRef pPath_0 = CGPathCreateMutable();
CGPathMoveToPoint(pPath_0, NULL, 72.61 / 2 + xCoordinates,20.21 / 2 + yCoordinates);
CGPathAddLineToPoint(pPath_0, NULL, 69.19 / 2 + xCoordinates,16.37 / 2 + yCoordinates);
CGPathAddLineToPoint(pPath_0, NULL, 26.02 / 2 + xCoordinates,16.37 / 2 + yCoordinates);
CGPathAddLineToPoint(pPath_0, NULL, 22.69 / 2 + xCoordinates,20.21 / 2 + yCoordinates);
CGPathAddCurveToPoint(pPath_0, NULL, 22.69 / 2 + xCoordinates,20.21 / 2 + yCoordinates,17.69 / 2 + xCoordinates,24.84 / 2 + yCoordinates,17.69 / 2 + xCoordinates,34.71 / 2 + yCoordinates);
CGPathAddLineToPoint(pPath_0, NULL, 17.69 / 2 + xCoordinates,194.87 / 2 + yCoordinates);
CGPathAddCurveToPoint(pPath_0, NULL, 17.69 / 2 + xCoordinates,194.87 / 2 + yCoordinates,20.87 / 2 + xCoordinates,206.37 / 2 + yCoordinates,47.69 / 2 + xCoordinates,206.37 / 2 + yCoordinates);
CGPathAddLineToPoint(pPath_0, NULL, 48.69 / 2 + xCoordinates,206.37 / 2 + yCoordinates);
CGPathAddCurveToPoint(pPath_0, NULL, 75.69 / 2 + xCoordinates,206.37 / 2 + yCoordinates,77.69 / 2 + xCoordinates,194.87 / 2 + yCoordinates,77.69 / 2 + xCoordinates,194.87 / 2 + yCoordinates);
CGPathAddLineToPoint(pPath_0, NULL, 77.69 / 2 + xCoordinates,34.71 / 2 + yCoordinates);
CGPathAddCurveToPoint(pPath_0, NULL, 77.69 / 2 + xCoordinates,24.84 / 2 + yCoordinates,72.61 / 2 + xCoordinates,20.21 / 2 + yCoordinates,72.61 / 2 + xCoordinates,20.21 / 2 + yCoordinates);
CGPathCloseSubpath(pPath_0);
CGContextSetRGBStrokeColor(context,0.0000,0.0000,0.0000,1.0000);
CGContextSetLineWidth(context, 1);
CGContextSetMiterLimit(context, 10.0000);

CGContextAddPath(context, pPath_0);
CGContextDrawPath(context, kCGPathStroke);
CGPathRelease(pPath_0);
CGContextRestoreGState(context);

我正在尝试使用以下代码用红色填充形状,但它不起作用:

CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
CGContextDrawPath(context, kCGPathFill);
CGContextFillPath(context);

我错过了什么?

1 个答案:

答案 0 :(得分:1)

修正了它:

CGContextAddPath(context, pPath_0);

CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
CGContextDrawPath(context, kCGPathFill);
CGContextFillPath(context);

CGContextDrawPath(context, kCGPathStroke);
CGPathRelease(pPath_0);
CGContextRestoreGState(context);