正如我的问题的标题所说..我试图用performSelectorOnMainThread
..我的函数 return调用一个非 void 函数UIImage
,当我这样调用它时:
UImage *img = [self performSelectorOnMainThread:@selector(captureScreen:) withObject:webView waitUntilDone:YES];
它给了我一个错误,我的分配类型不兼容,我也尝试像这样强制转换它
UImage *img = (UIImage*)[self performSelectorOnMainThread:@selector(captureScreen:) withObject:webView waitUntilDone:YES];
我得到了 语义问题:需要算术或指针类型的“void”类型的操作数
我知道我可以正常调用它,但是我尝试了一些多线程的东西并且需要它像这样调用..那么我该如何防止这个错误呢?
编辑 :
我尝试在此函数(captureScreen)中使用 GCD,它不断在“viewToCapture”上生成 Exc_bad_access .. 所以我决定在 GCD 块内调用父函数
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
[self captureScreenDelayed:pageView];
dispatch_sync(dispatch_get_main_queue(), ^{
});
});
和“ captureScreenDelayed :”如下
- (void) captureScreenDelayed:(EpubPageViewController*)pageView
{
if(!pageView)
pageView = [self currentPageView];
if(pageView.pageImageView)
pageView.pageImageView.image = (UIImage*)[self performSelectorOnMainThread:@selector(captureScreen:) withObject:webView waitUntilDone:YES];
}
和captureScreen:
-(UIImage*)captureScreen:(UIView*) viewToCapture
{
UIGraphicsBeginImageContextWithOptions(viewToCapture.bounds.size, viewToCapture.opaque, 0.0);
[viewToCapture.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return viewImage;
}
老实说,我不确定这是否是在单独的线程中获取图像的正确方法......我感谢任何建议。