从块中返回UIImage [英] returning UIImage from block

查看:119
本文介绍了从块中返回UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

- (UIImage *) getPublisherLogo
{
    //check the cache if the logo already exists
    NSString * imageUrl = [NSString stringWithFormat:@"%@/%@&image_type=icon", self.baseUrl, self.imageUrl_];


        ASIHTTPRequest * imageRequest = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:imageUrl]];
        [imageRequest setTimeOutSeconds:30.0];
        [imageRequest setDownloadCache:[ASIDownloadCache sharedCache]];
        [imageRequest setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
        [imageRequest setCachePolicy:ASIAskServerIfModifiedWhenStaleCachePolicy|ASIFallbackToCacheIfLoadFailsCachePolicy]; 
        [imageRequest setCompletionBlock:^(void){


            UIImage *img = [UIImage imageWithData:[imageRequest responseData] ];
            if (img){
                return img;
            }
        }];

        [imageRequest setFailedBlock:^(void){
            NSLog(@"Error in pulling image for publisher %@", [[imageRequest error] userInfo]);
        }];

        [imageRequest startAsynchronous];
    }
}

问题是返回值/ UIImage被返回在一个街区。我该如何避免这种情况?

The issue is that the return value/UIImage is returned at a block. How do I avoid this?

推荐答案

您无法从完成块返回任何内容,因为它已返回 void

You're unable to return anything from the completion block because it's returned void.

您可能需要创建一个新方法,如 setLogo:(UIImage *)image 关于期望设置图像的对象,并从完成块中调用该方法。

You'll probably need to create a new method like setLogo:(UIImage *)image on the object that's expecting the image to be set, and call that method from within the completion block.

这篇关于从块中返回UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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