使用performSelectorOnMainThread调用非void函数 [英] Invoke non void functions with performSelectorOnMainThread

查看:56
本文介绍了使用performSelectorOnMainThread调用非void函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如我的问题的标题所说..我尝试使用 performSelectorOnMainThread 调用非空函数..当我调用它时,我的函数返回 UIImage 像这样:

As the title of my question said .. I'm try to call a non-void function with performSelectorOnMainThread .. my function return UIImage , when I call it like this:

UImage *img = [self performSelectorOnMainThread:@selector(captureScreen:) withObject:webView waitUntilDone:YES];

它给我一个错误,指出我的分配类型不兼容,我也尝试像这样强制转换

it gives me an error that I have incompatible assigning type , also I try to cast it like this

UImage *img = (UIImage*)[self performSelectorOnMainThread:@selector(captureScreen:) withObject:webView waitUntilDone:YES];

并且我得到了语义问题:'void'类型的操作数,其中需要算术或指针类型

我知道我可以正常调用它,但是我正在尝试一些多线程的东西,需要它像这样调用..所以我如何防止这个错误?

I know that I can call it as normal , but Im trying some multithreading stuff and need it to invoke like this .. so how I can prevent this error please?

我尝试在此函数(captureScreen)中使用GCD,使它在"viewToCapture"上继续执行Exc_bad_access ..因此,我决定在GCD块中调用父函数

I try to use GCD inside this function (captureScreen) its keep making Exc_bad_access on the "viewToCapture" .. so I decide to call the parent function inside the GCD block

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;
}

说实话,我不确定这是否是在分开的线程中获取图像的正确方法...我感谢任何建议.

to be honest Im not sure if this the correct way to get the image in separated thread... I appreciate any advice.

推荐答案

您在这里要记住的是,您实际上正在调用的方法是 -performSelectorOnMainThread:withObject:waitUntilDone:,其返回类型为(void).因此,您无法获得通过此方法返回的执行选择器的结果.如果您需要执行这种多线程处理,则应查看

The thing you have to remember here is that the method that you're actually calling is -performSelectorOnMainThread:withObject:waitUntilDone: which has a (void) return type. Thus, you can't get the result of the performed selector returned through this method. If you need to do this type of multithreading, you should check out the concurrency programming guide and, in particular, grand central dispatch.

这篇关于使用performSelectorOnMainThread调用非void函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆