[英]How to renderInContext on background queue?
I'm trying to make snapshot of UIView
. 我正在尝试制作UIView
快照。 I doing this with 'standard' things, but, as I making high performance app, I want to run it on background queue. 我用“标准”的东西来做这个,但是,当我制作高性能的应用程序时,我想在后台队列中运行它。 I know that call from background thread UIKits isn't good. 我知道从后台线程UIKits调用不是很好。
In this method I was tried to walk around and make snap of layer: 在这种方法中,我试图四处走动并捕捉图层:
-(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);
}
Mostly it's working fine, but in some cases (when the myWebView
's UIViewController
isn't visible - browser tabs hide it) it throws me errors: 大多数情况下,它运行良好,但是在某些情况下(当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
So, what's wrong, why it's worked perfectly if UIViewController
is visible, and crashes so badly if it's transited off for another UIViewController
? 那么,怎么了?为什么在UIViewController
可见的情况下它可以完美工作,而在将其转换为另一个UIViewController
崩溃如此严重?
All the UIKit features are explicitly main-thread-only except a few of exceptions. 除了少数例外,所有UIKit功能都明确地仅是主线程的。 Many of UIGraphics~
series functions are the exception cases. UIGraphics~
系列函数中有许多都是例外情况。 Anyway, anything else are not exceptions. 无论如何,其他都不是例外。 I suspect these lines may cause some issues. 我怀疑这些行可能会引起一些问题。
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
self.view.frame.size.width
Then, it would be better to check other part of your code for accidental call of UI~
series features. 然后,最好检查代码的其他部分以防意外调用UI~
系列功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.