[英]How to renderInContext on background queue?
我正在嘗試制作UIView
快照。 我用“標准”的東西來做這個,但是,當我制作高性能的應用程序時,我想在后台隊列中運行它。 我知道從后台線程UIKits調用不是很好。
在這種方法中,我試圖四處走動並捕捉圖層:
-(void)caps{
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, .1 * NSEC_PER_SEC);
dispatch_queue_t myQueue = dispatch_queue_create("com.myapp.shot", NULL);
dispatch_after(popTime, myQueue, ^(void){
[self capture];
});
}
-(void)capture{
NSLog(@"Start");
float height;
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if(orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight){
height = 600;
}else{
height = 500;
}
dispatch_async(dispatch_get_main_queue(), ^{
/// in .h @property CALayer *liar;
if (!self.liar) {
self.liar = myWebView.scrollView.layer;
NSLog(@"MAIN THREAD %@", self.liar);
}
});
UIGraphicsBeginImageContext(CGSizeMake(self.view.frame.size.width, height));
NSLog(@"NEw THREAD %@", self.liar);
[self.liar renderInContext:UIGraphicsGetCurrentContext()];
self.sharedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSLog(@"Finish %@", self.sharedImage);
}
大多數情況下,它運行良好,但是在某些情況下(當myWebView
的UIViewController
不可見時-瀏覽器選項卡將其隱藏)會引發錯誤:
bool _WebTryThreadLock(bool), 0x1adb1730: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...
1 0x35f3b435 WebThreadLock
2 0x305b0b31 <redacted>
3 0x305b09ed <redacted>
4 0x305b0769 <redacted>
5 0x305b02cb <redacted>
6 0x30561353 <redacted>
7 0x301e7943 <redacted>
8 0x301e3167 <redacted>
9 0x30212425 <redacted>
10 0x30234f77 <redacted>
11 0x12f38f -[Browser capture]
12 0x12f10f __20-[Browser caps]_block_invoke
13 0x3896b0c3 <redacted>
14 0x3896fa47 <redacted>
15 0x3896c72f <redacted>
16 0x3896fe3d <redacted>
17 0x3896cf93 <redacted>
18 0x38970745 <redacted>
19 0x389709c5 <redacted>
20 0x38a9adff _pthread_wqthread
21 0x38a9acc4 start_wqthread
那么,怎么了?為什么在UIViewController
可見的情況下它可以完美工作,而在將其轉換為另一個UIViewController
崩潰如此嚴重?
除了少數例外,所有UIKit功能都明確地僅是主線程的。 UIGraphics~
系列函數中有許多都是例外情況。 無論如何,其他都不是例外。 我懷疑這些行可能會引起一些問題。
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
self.view.frame.size.width
然后,最好檢查代碼的其他部分以防意外調用UI~
系列功能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.