2013-07-16 28 views
0

我想填充形状的内部,但没有用处。它只是描绘路径,但不填充你好颜色。我GOOGLE了,但没有作为填充不工作。指导我做错了什么。填充内部的路径保持线宽可见

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); 

编辑:为伯提的代码是

- (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; 

}

我也创造其他形状和路径在相同的方式进行。 类给我从开始和结束点的线。请告诉我,如果有什么我失踪。

+0

你在看什么?只有红色的轮廓? (你实际看到的图像+预计可能会有用) –

+0

只是红色的轮廓。 – fibnochi

+0

我试过了,我能够得到边框并使用相同的代码分隔填充颜色。我用这个为你的“pathi” CGPathRef pathi = CGPathCreateWithEllipseInRect(CGRectMake(0,0,100,100),nil); 现在我的猜测是要么你的路径不是一个大的矩形,或者你正在使用较大的值的宽度大小,这是隐藏你的填充颜色。 –

回答

0

我也reccomend哪些用户在朴子他的评论暗示:

存储的路径,第一填充(CGContextFillPath),改变笔触颜色,然后行程(CGContextStrokePath)它。
我已经这样做了,这工作正常。