从块返回 UIImage [英] returning UIImage from block
本文介绍了从块返回 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屋!
查看全文