UIGraphicsGetCurrentContext() 有什么问题?

发布于 2024-10-26 12:47:03 字数 2549 浏览 4 评论 0原文

我正在构建一个游戏,因此在我的结构中,我有 Game 类要求元素绘制自己。这是通过继承自 UIView 的类来完成的,该类在 Game 类的实例上调用 draw 方法

//jeu 是 Game 的一个实例

- (void)drawRect:(CGRect)rect {
    // Drawing code

    CGContextRef context = UIGraphicsGetCurrentContext();
    [jeu draw:context];
}

//因此 Game 方法要求所有对象为自己绘制

-(void) draw: (CGContextRef) gc{
    GameElement* element ;

    for( element in drawArr )
    {
        [element draw:(CGContextRef) gc];
    }
}

一些元素(一个球和两个边框,它们是全部由超类 GameElement 继承)正确绘制,但有些边框不正确

这是边框的绘制方法

- (void) draw: (CGContextRef) gc
{       
    CGFloat black[4] = {0.0f, 0.0f, 0.0f, 1.0f}; // R G B + Alpha
    CGPoint p [2];
    p[0].x = xStart ;
    p[0].y = yStart ;
    p[1].x = xEnd ;
    p[1].y = yEnd ;
    CGContextSetStrokeColor(gc, black); //Definie la couleur
    CGContextStrokeLineSegments(gc, p, 2 ) ;
}

所以在控制台日志上我读到:

Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextDrawImage: invalid context 0x0
Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextSetStrokeColor: invalid context 0x0
Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextStrokeLineSegments: invalid context 0x0
Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextSetStrokeColor: invalid context 0x0
Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextStrokeLineSegments: invalid context 0x0
Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextSetStrokeColor: invalid context 0x0
Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextStrokeLineSegments: invalid context 0x0
Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextSetStrokeColor: invalid context 0x0
Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextStrokeLineSegments: invalid context 0x0
Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextSetStrokeColor: invalid context 0x0
Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextStrokeLineSegments: invalid context 0x0

我不知道它到底意味着什么。如果 ViewDesigner 中创建的图形上下文对于所有对象都是相同的,为什么某些对象会自行绘制而其他对象则不会?

谢谢大家

I'm building up a game so in my structure I have the Game class asking to elements to draw themselves. This is done by a class inherited from UIView which calls the draw method on an instance of Game class

//jeu is an istance of Game

- (void)drawRect:(CGRect)rect {
    // Drawing code

    CGContextRef context = UIGraphicsGetCurrentContext();
    [jeu draw:context];
}

//so the Game method ask all objects to draw themselves

-(void) draw: (CGContextRef) gc{
    GameElement* element ;

    for( element in drawArr )
    {
        [element draw:(CGContextRef) gc];
    }
}

some elements (a ball and two borders which are all inherited by super class GameElement) correctly draw but some border doesn't

here's the draw method of borders

- (void) draw: (CGContextRef) gc
{       
    CGFloat black[4] = {0.0f, 0.0f, 0.0f, 1.0f}; // R G B + Alpha
    CGPoint p [2];
    p[0].x = xStart ;
    p[0].y = yStart ;
    p[1].x = xEnd ;
    p[1].y = yEnd ;
    CGContextSetStrokeColor(gc, black); //Definie la couleur
    CGContextStrokeLineSegments(gc, p, 2 ) ;
}

So on the console log I read:

Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextDrawImage: invalid context 0x0
Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextSetStrokeColor: invalid context 0x0
Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextStrokeLineSegments: invalid context 0x0
Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextSetStrokeColor: invalid context 0x0
Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextStrokeLineSegments: invalid context 0x0
Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextSetStrokeColor: invalid context 0x0
Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextStrokeLineSegments: invalid context 0x0
Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextSetStrokeColor: invalid context 0x0
Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextStrokeLineSegments: invalid context 0x0
Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextSetStrokeColor: invalid context 0x0
Thu Mar 24 21:53:59 MacBook-Air-di-Michele-Giarratana.local IVBricker[381] <Error>: CGContextStrokeLineSegments: invalid context 0x0

And I have no idea of what the hell it means. If the graphic context created in ViewDesigner is the same for all why certains objects draw themselves while others don't?

Thank you all

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

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

发布评论

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

评论(1

撕心裂肺的伤痛 2024-11-02 12:47:03

我们知道您的上下文正在变得无效。检查每个 GameElement 绘制方法中是否 gc == nil ,直到找到第一个具有无效上下文的方法。

UIView将在调用drawRect:之前设置当前上下文,但不要手动调用drawRect,而是使用setNeedsDisplay方法。

上下文可能会死亡的其他方式,调用 UIGrhicsPushContext()

我想在确定它在哪里失效之后,你就会有线索。

We know your context is becoming invalid. Check if gc == nil in each GameElement draw method, until you find the first one with a invalid context.

UIView will setup the current context before calling drawRect:, but don't call drawRect manually, use the setNeedsDisplay method instead.

Other ways the context can die, calling UIGrhicsPushContext()

I think after determining where it's invalidating, you'll have a clue.

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