xcode CGContextSaveGState:无效上下文 0x0 错误仅在设备上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32236204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:09:15  来源:igfitidea点击:

CGContextSaveGState: invalid context 0x0 Error only on device

xcodecore-graphicsxcode7

提问by mattgabor

Update: I am using XCode 7 Beta with Swift 2.0

更新:我在 Swift 2.0 中使用 XCode 7 Beta

When I try to run my app on an iPhone I get these 3 errors:

当我尝试在 iPhone 上运行我的应用程序时,我收到以下 3 个错误:

<Error>: CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
<Error>: CGContextTranslateCTM: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
<Error>: CGContextRestoreGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

When I enable to CG_CONTEXT_SHOW_BACKTRACE variable I get this:

当我启用 CG_CONTEXT_SHOW_BACKTRACE 变量时,我得到了这个:

Backtrace:
      <<redacted>+40>
       <<redacted>+432>
        <<redacted>+144>
         <<redacted>+172>
          <<redacted>+332>
           <<redacted>+112>
            <<redacted>+820>
             <<redacted>+688>
              <<redacted>+356>
               <<redacted>+80>
                <<redacted>+808>
                 <<redacted>+344>
                  <<redacted>+420>
                   <<redacted>+104>
                    <<redacted>+284>
                     <<redacted>+556>
                      <<redacted>+504>
                       <<redacted>+1792>
                        <<redacted>+220>
                         <<redacted>+284>
                          <<redacted>+920>
                           <<redacted>+168>
                            <<redacted>+184>
                             <<redacted>+56>
                              <<redacted>+24>
                               <<redacted>+540>
                                <<redacted>+724>
                                 <CFRunLoopRunSpecific+384>
                                  <<redacted>+460>
                                   <UIApplicationMain+204>
                                    <main+164>

I'm not sure how to interpret this, I am not using any CoreGraphics content in my code, and the other similar questions had answers that did not work for me. I understand it can be difficult to help debug without code, but I can't figure out what code is related to this, so if anyone has an idea I can post more code.

我不确定如何解释这一点,我没有在我的代码中使用任何 CoreGraphics 内容,其他类似问题的答案对我不起作用。我知道在没有代码的情况下很难帮助调试,但我无法弄清楚哪些代码与此相关,因此如果有人有想法,我可以发布更多代码。

回答by Mark Krenek

From my experimentation, this seems to be due to defining UIViewControllerBasedStatusBarAppearancein Info.plist.

从我的实验来看,这似乎是由于UIViewControllerBasedStatusBarAppearanceInfo.plist.

It happens on iOS 9 (beta & GM) but not on 8.4.

它发生在 iOS 9(测试版和 GM)上,但不在 8.4 上。

回答by Olie

At @JimRota's suggestion, moving this into an answer, since it was deemed helpful:

在@JimRota 的建议下,将其转化为答案,因为它被认为有帮助:

Had this still in XCode 7 GM, iOS9 GM. But, check it out: Set UIViewControllerBasedStatusBarAppearanceto YES (even though the status bar is hidden, which is why I set it to NO to begin with), and the warnings go away. Crazy!

在 XCode 7 GM、iOS9 GM 中还有这个。但是,检查一下:设置UIViewControllerBasedStatusBarAppearance为 YES(即使状态栏是隐藏的,这就是我将其设置为 NO 开始的原因),警告就会消失。疯狂的!

I then had to add this to my view controllers to keep the status bar hidden:

然后我不得不将它添加到我的视图控制器中以保持状态栏隐藏:

- (BOOL) prefersStatusBarHidden
{
    return YES;
}

For me, all my VCs actually inherit from the ProjectNameViewController, so that simplified it a bit.

对我来说,我所有的 VC 实际上都继承自ProjectNameViewController,因此简化了一点。

回答by Logic

from comments given above , this is the real way to fix :

根据上面给出的评论,这是解决问题的真正方法:

UIViewControllerBasedStatusBarAppearance to YES in Info.plist

回答by user3099837

im getting the same thing on the xcode 7 GM release, i think ill simply ignore them since everything works but its pretty annoying even though they have admitted its a bug and just didnt fix it yet. Dev Forum Posting

我在 xcode 7 GM 版本上得到了同样的东西,我认为我不会简单地忽略它们,因为一切正常,但它很烦人,即使他们承认它是一个错误并且只是没有修复它。 开发论坛发帖

回答by Peter Johnson

I am getting this too (including the redacted part) when compiling my project on iOS9 beta5. Its fine when compiling on iOS8.

在 iOS9 beta5 上编译我的项目时,我也得到了这个(包括编辑的部分)。在iOS8上编译时没问题。

It happens right in my App Delegate, before executing any commands from

它发生在我的 App Delegate 中,在执行任何命令之前

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

so it seems more likely to be an iOS beta problem than ours.

所以它似乎比我们的更有可能是 iOS 测试版问题。

回答by madewulf

I had the same type of bug when using non integer values as insets for resizable images, like this:

使用非整数值作为可调整大小的图像的插图时,我遇到了相同类型的错误,如下所示:

UIImage * image = [[UIImage imageNamed:@"pill"]
             resizableImageWithCapInsets:UIEdgeInsetsMake(17.5, 17.5, 17.5, 17.5)];

This caused this kind of logs. Using integer values fixed the problem.

这导致了这种日志。使用整数值解决了这个问题。

回答by Lee

I had this error. It was because I created a function and forgot to add a return value.

我有这个错误。那是因为我创建了一个函数,忘记添加返回值。

-(void)doSomething

vs.

对比

-doSomething

ugh.

啊。