Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can't fill my CGPath

I need some help with filling my path. I have no clue where to put CGContextFillPath(path); for example. I am pretty new to drawing (and iOS Development) and I really tried everything for nearly two hours now. But I reached the point where I just have to give up... Hope you can shed some light on my problem. I get the following error : 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;
}
like image 469
the_critic Avatar asked Nov 29 '11 14:11

the_critic


2 Answers

It should take a CGContext as the parameter, not a CGPath.

CGContextFillPath(context);

However, the call to CGContextStrokePath cleared the current path, so the CGContextFillPath will do nothing. To stroke and fill on the same path, you should use CGContextDrawPath:

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

(and also apply @Luiz's change about getting the context.)

like image 174
kennytm Avatar answered Oct 06 '22 01:10

kennytm


You need to get the context before adding a path to it:

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

Also, to fill the path, you need to pass the context reference, not the path itself:

CGContextFillPath(context);
like image 21
Luiz Carlos Querino Filho Avatar answered Oct 06 '22 00:10

Luiz Carlos Querino Filho