objective-c CG填充路径不起作用

时间:2012-08-06 18:41:15

标签: objective-c cg

我有以下问题:

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetLineCap(context,kCGLineCapRound);
CGContextSetLineJoin(context, kCGLineJoinRound);
CGContextSetRGBFillColor(context, 1, 0, 0, 1);

for (drawnCurve *line in completeLines) {
CGContextBeginPath(context);    
    for(int i=0;i<[line.points count]-1;i++) 
    { 
    drawnPoint*point=[line.points objectAtIndex:i];
    drawnPoint*point2=[line.points objectAtIndex:i+1];

   // CGContextSetLineWidth(context, point.r);
    [point.color set];
    CGContextMoveToPoint(context, point.x,point.y);
        CGContextAddLineToPoint(context, point2.x, point2.y);  
    // CGContextAddCurveToPoint(context, (point.x+point2.x)/2, point2.y, (point.x+point2.x)/2, point2.y, point2.x, point2.y);

        //CGContextEOFillPath(context);
    }
    CGContextClosePath(context);
    CGContextFillPath(context);
}

但它只是消失而没有显示任何填充曲线。问题是什么?

1 个答案:

答案 0 :(得分:1)

CGContextMoveToPoint的调用开始一个新的子路径。您应该在嵌套的for循环之前调用一次,然后仅使用CGContextAddLineToPoint来保留行连接:

drawnPoint*pointZero=[line.points objectAtIndex:0];
CGContextMoveToPoint(context, pointZero.x,pointZero.y);
for(int i=1;i<[line.points count];i++) 
{ 
    drawnPoint*point=[line.points objectAtIndex:i];
    CGContextAddLineToPoint(context, point.x, point.y);  
}
CGContextClosePath(context);
CGContextFillPath(context);