我无法填写我的 CGPath

发布于 2024-12-18 17:14:27 字数 2473 浏览 5 评论 0原文

我需要一些帮助来填补我的道路。例如,我不知道在哪里放置 CGContextFillPath(path); 。我对绘图(和 iOS 开发)还很陌生,现在我真的尝试了所有的东西近两个小时。但我已经到了不得不放弃的地步......希望你能阐明我的问题。我收到以下错误:上下文无效

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

为你拒绝所有暧昧 2024-12-25 17:14:27

它应该采用 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 关于获取上下文的更改。)

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

(り薆情海 2024-12-25 17:14:27

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

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

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

CGContextFillPath(context);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文