我无法填写我的CGPath

时间:2011-11-29 14:59:48

标签: objective-c cgpath

我需要一些帮助来填补我的道路。我不知道在哪里放CGContextFillPath(path);例如。我是绘图(和iOS开发)的新手,我现在已经尝试了近两个小时了。但是我达到了我必须放弃的程度......希望你能解释一下我的问题。我收到以下错误:invalid context

-(CGMutablePathRef) drawHexagon:(CGPoint)origin
    {
        //create mutable path
        CGMutablePathRef path = CGPathCreateMutable();

        CGPathMoveToPoint(path, nil, origin.x, origin.y);

        CGPoint newloc = CGPointMake(origin.x - 20, origin.y + 42);
        CGPathMoveToPoint(path, NULL, newloc.x, newloc.y);
        CGPathAddLineToPoint(path, NULL, newloc.x + 16,newloc.y + 38);
        CGPathAddLineToPoint(path, NULL, newloc.x + 49, newloc.y + 0);
        CGPathAddLineToPoint(path, NULL, newloc.x + 23,  newloc.y - 39);
        CGPathAddLineToPoint(path, NULL, newloc.x - 25,newloc.y - 40);
        CGPathAddLineToPoint(path, NULL, newloc.x -43, newloc.y + 0);
        CGPathCloseSubpath(path);
        CGContextAddPath(context, path);
        context = UIGraphicsGetCurrentContext();
        CGContextSaveGState(context);
        CGContextSetLineWidth(context, 2.0);
        CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
        CGContextAddPath(context, path);
        CGContextStrokePath(context);
        CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
        CGContextFillPath(path);



           return path;   

    }

    -(id) init
    {
        // always call "super" init
        // Apple recommends to re-assign "self" with the "super" return value
        if( (self=[super init])) {

           clickedHexagonsUpToNow = [[NSMutableArray alloc]init ];
            CGSize screenSize = [CCDirector sharedDirector].winSize;

            background = [CCSprite spriteWithFile:@"background.png"];
            background.anchorPoint= ccp(0,0);
            [self addChild:background z:-1 tag:0];

            hex1TouchArea = [self drawHexagon:ccp(82,120)];
            hex2TouchArea = [self drawHexagon:ccp(50,157)];
            hex3TouchArea = [self drawHexagon:ccp(220 ,196)];
            hex4TouchArea = [self drawHexagon:ccp(280,153)];
            hex5TouchArea = [self drawHexagon:ccp(84,118)];
            hex6TouchArea = [self drawHexagon:ccp(82,120)];
            hex7TouchArea = [self drawHexagon:ccp(82,120)];
            hex8TouchArea = [self drawHexagon:ccp(82,120)];
            hex9TouchArea = [self drawHexagon:ccp(82,120)];


             self.isTouchEnabled = YES;

            }
    return self;
}

2 个答案:

答案 0 :(得分:9)

它应该将CGContext作为参数,而不是CGPath。

CGContextFillPath(context);

但是,对CGContextStrokePath的调用清除了当前路径,因此CGContextFillPath将不执行任何操作。要描边和填充相同的路径,您应该使用CGContextDrawPath

    ...
    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
    CGContextAddPath(context, path);
//  CGContextStrokePath(context);
    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
//  CGContextFillPath(path);
    CGContextDrawPath(context, kCGPathFillStroke);

(并且还应用@Luiz关于获取上下文的更改。)

答案 1 :(得分:1)

您需要在添加路径之前获取上下文:

context = UIGraphicsGetCurrentContext();    
CGContextAddPath(context, path);

此外,要填充路径,您需要传递上下文引用,而不是路径本身:

CGContextFillPath(context);