[英]why using NSOperation, GCD or performSelectorInBackground I can't take snapshot of UIView?
我正在嘗試使用以下方法獲取UIView
screenshotot。 僅當我不嘗試在“后台”執行此操作時,它才有效。 但是我需要在后台使用它,因為每次啟動時,我都會承擔可怕的ui責任-幾秒鍾內什么都不會起作用。
當我暫停此方法時,此方法有效,但會永久阻塞,並帶有mach_msg_trap
消息。
-(void)takeShoot{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
float height;
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if(orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight){
height = 600;
}else{
height = 500;
}
UIGraphicsBeginImageContextWithOptions(CGSizeMake(self.view.frame.size.width, height), YES, 0.6);
[self.view drawViewHierarchyInRect:CGRectMake(0, -60, self.view.frame.size.width, self.view.frame.size.height) afterScreenUpdates:YES];
self.sharedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
});
}
當我在NSOperation
被NSOperation
,情況甚至變得更糟-它會無緣無故地這樣做。 第三種方法performSelectorInBackground
只是垃圾,如果我每次在uiwebview
完成加載時都觸發該方法,那么將uiwebview
十億個線程,那就是我認為一點也不好。
因此,如何在后台(不在mainThread
)進行屏幕截圖,而不殺死或凍結任何人?
您必須在主線程上截屏。 這是UI穩定的唯一線程。
如果您有幾秒鍾的問題,我懷疑您還有其他問題,這只是在暴露。 最有可能是您在后台線程上進行的其他UIKit調用。 不支持該功能,並且會導致您描述的症狀。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.