我有一個應用程序,屏幕在後臺線程中不斷捕獲。這裏是代碼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