我有一个应用程序,屏幕在后台线程中不断捕获。这里是代码objective c renderInContext在后台线程崩溃
- (UIImage *) captureScreen {
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
CGRect rect = [keyWindow bounds];
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[[keyWindow layer] renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIDeviceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if ((orientation == UIInterfaceOrientationLandscapeLeft) || (orientation == UIInterfaceOrientationLandscapeRight) || (orientation==UIInterfaceOrientationPortraitUpsideDown)) {
img=[self rotatedImage:img];
}
return img;
}
它适用于捕获一次或两次。但过了一段时间,应用程序崩溃总是在同一行[[keyWindow layer] renderInContext:context];
,它给出EXC_BAD_ACCESS (code=1, address=0x8)
消息。我到处搜索,没有任何用处。只发现renderInContext在后台线程中工作时存在内存泄漏问题。但正如你所理解的,并不能解决我的问题:)。 因此有3个问题: -
这个崩溃(问题)的原因是什么?
我该怎么办?
是否有任何其他方式来捕捉屏幕(旁边苹果建议,因为renderInContext也被使用)。没有渲染的东西...?
您应该在主线程 – nsgulliver 2013-02-28 12:13:38
上执行您的GUI相关任务向我们展示您的渲染代码。你是否试图在未创建它们的线程中使用核心数据对象。你在渲染代码中使用了非原子属性吗?这些和更多都可能导致这种情况。 – 2013-02-28 12:38:39
当调用'renderInContext'时,是否有'layer'或'context' nil?是否调用了receivedMemoryWarning方法? – 2013-02-28 13:15:30