【发布时间】:2012-04-25 04:30:08
【问题描述】:
我正在使用以下内容从 UIView 生成 UIImage
UIGraphicsBeginImageContextWithOptions(view.frame.size, YES, [[UIScreen mainScreen] scale]);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage* img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
这通常有效,但大约有一半的时间,当调用 renderInContext 时出现此错误:
<Error>: CGContextDrawImage: invalid context 0x84d0b20
有谁知道为什么会发生这种情况或如何检测它何时发生?我想如果上下文的地址是 0x0 我会感觉更好,因为它至少是我可以测试和处理的东西,但到目前为止这让我很难过。
编辑:哎呀。意味着使用 view.frame.size 而不是 view.bounds.size。
【问题讨论】:
-
你明白了吗?我也收到非空无效上下文错误...我从
UIGraphicsGetImageFromCurrentImageContext()得到的 UIImage 看起来不错... -
很遗憾,没有。我想我将不得不求助于重新绘制我试图用绘图命令捕获的东西。
-
@minovsky 我遇到了同样的上下文问题,但我的情况有点不同。无论如何,就我而言,这是 UIGraphicsPushContext/UIGraphicsPopContext 平衡的问题。我弹出的不仅仅是推送,当我尝试对该上下文执行任何操作时(即使它在使用之前被推送) - 出现了 Invalid Context 错误。
标签: iphone uiview uiimage cgcontext