CGContext无效上下文0x0。

24

: CGContextSetFillColorWithColor: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.

: CGContextSetStrokeColorWithColor: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.

类似的错误会有:

  • CGContextSaveGState
  • CGContextSetFlatness
  • CGContextAddPath
  • CGContextDrawPath
  • CGContextRestoreGState
  • CGContextSaveGState
  • CGContextSetFlatness
  • CGContextAddPath
  • CGContextDrawPath
  • CGContextRestoreGState
  • CGContextSetFillColorWithColor
  • CGContextSetStrokeColorWithColor
  • CGContextSetFillColorWithColor
  • CGContextSetStrokeColorWithColor
  • CGContextGetBlendMode
  • CGContextSetBlendMode
  • 等等……

我的应用程序中没有使用任何CGContext相关的内容。我只有一个UITextView,一个UIButton和一个UILabel,并且还没有添加太多代码。只是通过以下方式设置UITextView的内容:

[self.text_view setText:@"123"];

如何解决错误?

我正在使用Xcode 5.0(版本号5A1413),iOS 7模拟器(64位Retina 4“)和Mac OS X 10.8.5。


我可以在UITextField上重现此警告,但在UITextView上却不能。该警告仍然在iOS 7.0.3中发生。 - bilobatum
谢谢提供的信息。我从其他帖子中了解到UITextField也受到影响,但是没有人提供有效的解决方案。我认为这是一个误报。 - Raptor
这个问题解决了吗?我目前正在为iOS开发Unity3D游戏,我们得到了一个非常类似的CG相关无效状态警告输出,然后应用程序就会崩溃。它只是偶尔发生,并且仅在触发本地对话框时发生(我们正在尝试获取本地对话框插件的源代码,但从阅读这篇文章来看,似乎更像是iOS渲染文本框的问题?) - Fat-Wednesday
这是iOS 7的已知错误:https://dev59.com/z2Ik5IYBdhLWcg3wFKlK - user3089080
这是目前在iOS中存在的一个问题,目前还没有任何解决方案。有关更多信息,请访问以下链接: https://dev59.com/z2Ik5IYBdhLWcg3wFKlK - Roy Jay
显示剩余3条评论
3个回答

46
尝试以下操作:在xcode中添加符号断点到CGPostError。(添加符号断点,将符号字段输入CGPostError)
当错误发生时,调试器将停止代码执行并可以检查方法调用的堆栈和参数。

3
我希望我能给你一百个赞。这帮助我解决了一个长期存在的问题,我不知道这些信息来自哪里。谢谢! - Sean Reilly
太棒了,伙计 - 你刚刚用这个技巧为我节省了大量时间!! - ATV
这个断点将我带到了任何视图控制器加载之前的汇编代码。可能是苹果的一个错误吗? - Bagusflyer
是的,非常可能,特别是在Xcode 7 beta上 ;) - Juraj Antas

2

在使用CGContext和UIBezierPaths时,我遇到了类似的错误。我采用了Juraj Antas的方法,很快找到了问题的原因。

我正在使用CGContext在UIImageView上绘制图形。首先,我创建了一个UIBezierPath,然后使用CGContextAddPath(..)进行绘制。我发现当我创建UIBezierPath时设置颜色,Xcode不喜欢这样,并且颜色也会被CGContextSetStrokeColorWithColor(..)覆盖掉。只要我从UIBezierPath创建代码中删除UIColor.whiteColor().set(),错误就消失了。


0

感谢 @shnaz 的建议。

我也遇到了同样的问题,花费了一些时间才找到问题所在。希望我的解决方案能够帮助其他人解决同样的问题。

可能是因为我没有仔细阅读文档,而且我刚开始接触创建UIBezierPath。结果在XCode6中,.stroke().fill()有时会导致无效的上下文0x0。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接