空图形上下文

4

这是我正在使用的代码片段:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^
{

    UIGraphicsBeginImageContextWithOptions(irect.size, YES, 0.0 );
    CGContextRef context = UIGraphicsGetCurrentContext();
    if (context == 0)
        NSLog (@"Null Graphics Context") ;
    else
        NSLog (@"OK") ;

    . . . .
    // Various drawing functions

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
  }) ;

我发现每隔一次调用UIGraphicsGetCurrentContext就会返回NULL。我在控制台上看到了这个消息。
CGContextRestoreGState: invalid context 0x0. 这是一个严重的错误。这个应用程序或它使用的库正在使用一个无效的上下文,从而对系统稳定性和可靠性造成了总体的损害。这个通知是出于礼貌,请解决这个问题。它将在即将发布的更新中变成致命错误。
什么原因会导致UIGraphicsGetCurrentContext返回无效的图形上下文?

你的问题中缺乏足够的信息。你看过这篇帖子了吗?https://dev59.com/JGct5IYBdhLWcg3wAY3q - Anthony Kong
1个回答

16

根据评论建议,答案是传递给 UIGraphicsBeginImageContextWithOptions CGRect 区域为零。这显然导致无法创建图形上下文。


我曾经遇到过同样的问题,答案就是检查宽度和高度是否都不为0。 - MLBDG

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