gpt4 book ai didi

iphone - 拥有一个包含许多子层的 CALayer。当在子图层上调用 setNeedsDisplay 时,我收到无效上下文错误

转载 作者:行者123 更新时间:2023-12-03 19:39:17 25 4
gpt4 key购买 nike

我刚刚重写了我的游戏以使用核心动画,以使其更快。我有一个 UIView,其中包含许多子层(游戏“图 block ”)。图 block 子图层实际上是 CALayer 子类,它们使用 drawInContext: 绘制自己的内容。例如,当选择一个图 block 时,需要重新绘制它,因此我调用 [tileLayer setNeedsDisplay] 并收到一堆无效上下文错误。如果我调用 [mainView setNeedsDisplay][mainView.layer setNeedsDisplay],则不会重绘任何图 block 。如何让单个图 block (CALayer 子类)重绘?

编辑:从非常有用的评论中,我发现这只在 CALayer 子类绘制图像时才会发生。以下是错误:

<Error>: CGContextSaveGState: invalid context 0x0
<Error>: CGContextSetBlendMode: invalid context 0x0
<Error>: CGContextSetAlpha: invalid context 0x0
<Error>: CGContextTranslateCTM: invalid context 0x0
<Error>: CGContextScaleCTM: invalid context 0x0
<Error>: CGContextDrawImage: invalid context 0x0
<Error>: CGContextRestoreGState: invalid context 0x0

这是导致错误的代码行:

[[UIImage imageNamed:@"flag.png"] drawInRect:self.bounds];

还有其他方法可以在 CALayer 子类中绘制图像吗?

最佳答案

Core Animation 不了解 UIKit,即 drawInContext: 不会将 CALayer 上下文设置为 UIKit 的当前上下文。 UIImage 中的drawInRect: 方法在此当前上下文中绘制图像。要手动将上下文推送到 UIKit,请使用 UIGraphicsPushContext() 并在完成后使用 UIGraphicsPopContext() 弹出它。

例如:

UIGraphicsPushContext(context);
[[UIImage imageNamed:@"flag.png"] drawInRect:self.bounds];
UIGraphicsPopContext();

关于iphone - 拥有一个包含许多子层的 CALayer。当在子图层上调用 setNeedsDisplay 时,我收到无效上下文错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6721961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com