【发布时间】:2013-07-16 12:06:03
【问题描述】:
我正在尝试填充形状的内部,但没有用。它只描边路径但不填充你好颜色。我已经用谷歌搜索了,但没有任何东西作为填充不起作用。指导我做错了什么。
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextAddPath(ctx, pathi);
CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);
CGContextSetFillColorWithColor(ctx, [UIColor yellowColor].CGColor);
CGContextSetLineWidth(ctx, 3.0);
CGContextSetLineCap(ctx, kCGLineCapSquare);
CGContextSetAllowsAntialiasing(ctx, NO);
// CGContextStrokePath(ctx);
CGContextDrawPath(ctx, kCGPathFillStroke);
编辑: pathi 的代码是
- (CGMutablePathRef)getPath {
if (self.shapeType == NOSHAPE) {
return nil;
}
[lineHeight removeAllObjects];
[linesArray removeAllObjects];
CGMutablePathRef path = CGPathCreateMutable();
switch (self.shapeType) {
case Rectangle_SHAPE:{
Line *l1 = [[Line alloc] initWithLineName:LineName_a_S
starttPoint:self.bound.origin
endPoint:CGPointMake(self.bound.size.width, self.bound.origin.y) scalingFactor:1.0
linePosition:LINE_DIRECTION_HORIZONTAL];
[lineHeight addObject:[NSNumber numberWithFloat:l1.lineLength]];
[linesArray addObject:l1];
Line *l2 = [[Line alloc] initWithLineName:LineName_c_S
starttPoint:CGPointMake(self.bound.size.width, self.bound.origin.y)
endPoint:CGPointMake(self.bound.size.width, self.bound.size.height) scalingFactor:1.0
linePosition:LINE_DIRECTION_VERTICAL];
[lineHeight addObject:[NSNumber numberWithFloat:l2.lineLength]];
[linesArray addObject:l2];
Line *l3 = [[Line alloc] initWithLineName:LineName_b_S
starttPoint:CGPointMake(self.bound.size.width, self.bound.size.height)
endPoint:CGPointMake(self.bound.origin.x, self.bound.size.height) scalingFactor:1.0
linePosition:LINE_DIRECTION_HORIZONTAL];
[lineHeight addObject:[NSNumber numberWithFloat:l3.lineLength]];
[linesArray addObject:l3];
Line *l4 = [[Line alloc] initWithLineName:LineName_d_S
starttPoint:CGPointMake(self.bound.origin.x, self.bound.size.height)
endPoint:self.bound.origin scalingFactor:1.0
linePosition:LINE_DIRECTION_VERTICAL];
[lineHeight addObject:[NSNumber numberWithFloat:l4.lineLength]];
[linesArray addObject:l4];
CGPathAddPath(path, NULL, [l1 getPath]);
CGPathAddPath(path, NULL, [l2 getPath]);
CGPathAddPath(path, NULL, [l3 getPath]);
CGPathAddPath(path, NULL, [l4 getPath]);
[l1 release];
[l2 release];
[l3 release];
[l4 release];
break;
}
default:
break;
}
return path;
}
我也在创建其他形状,路径也是以同样的方式制作的。 Line 类为我提供了起点和终点的线。请告诉我是否有我遗漏的东西。
【问题讨论】:
-
你在看什么?只有红色轮廓? (您实际看到的 + 预期的图像可能很有用)
-
我试过了,我能够通过使用相同的代码来分离边框和填充颜色。我将它用于您的“pathi” CGPathRef pathi = CGPathCreateWithEllipseInRect(CGRectMake(0, 0, 100, 100), nil);现在我的猜测是您的路径不是一个大矩形,或者您使用的宽度尺寸较大,它隐藏了您的填充颜色。
-
@Puneet 我还通过绘制矩形进行了检查,它正在工作,但不适用于我的路径。 pathi 在整个矩形上发送垃圾邮件,边距为 1.5。我也尝试过默认宽度,但这也不起作用。如果你想看看我是如何创建 pathi 的,我会分享代码。
-
请。分享您正在创建路径的代码
-
为什么不直接使用
CGContextFillPath和CGContextStrokePath?然后你可以填充并绘制边界线。不过,请确保按此顺序进行操作。
标签: iphone ios core-graphics quartz-graphics cgpath