2014-04-07 35 views
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:無效的上下文0x0。這是一個嚴重的錯誤。此應用程序或其使用的庫正在使用無效的上下文,從而導致系統穩定性和可靠性的整體降級。這個通知是禮貌的:請解決這個問題。這將成爲即將到來的更新中的致命錯誤。

什麼會導致UIGraphicsGetCurrentContext返回無效的圖形上下文?

+0

您的問題中沒有足夠的信息。你有沒有看過這篇文章? http://stackoverflow.com/questions/12424690/uigraphicsgetcurrentcontext-seems-to-return-nil –

回答

14

正如評論建議,答案是CGRect傳遞給UIGraphicsBeginImageContextWithOptions有零區。這顯然會導致它無法創建圖形上下文。

+0

我有同樣的問題,這是答案,檢查寬度和高度是不是0。 – MLBDG