Ios 如何从GCD返回UIImage类型
我有一个名为“ComLog”的“UIImage”返回类型方法。我想从这个方法返回一个图像。在“ComLog”方法中,我使用GCD从数组中获取图像值。我使用以下代码,“NSLog(@“QQQQQ%@”,exiIco)”打印“图像”值,但NSLog(@“QQQQQQ%@,exiIco)不打印;“不要。 详情如下:Ios 如何从GCD返回UIImage类型,ios,objective-c,uiimageview,uiimage,grand-central-dispatch,Ios,Objective C,Uiimageview,Uiimage,Grand Central Dispatch,我有一个名为“ComLog”的“UIImage”返回类型方法。我想从这个方法返回一个图像。在“ComLog”方法中,我使用GCD从数组中获取图像值。我使用以下代码,“NSLog(@“QQQQQ%@”,exiIco)”打印“图像”值,但NSLog(@“QQQQQQ%@,exiIco)不打印;“不要。 详情如下: -(UIImage*) ComLog { ExibitorInfo *currentExibitor100 = [[ExibitorInfo alloc] init]; currentEx
-(UIImage*) ComLog
{
ExibitorInfo *currentExibitor100 = [[ExibitorInfo alloc] init];
currentExibitor100 = [self.exibitorsArray objectAtIndex:0];
imageQueueCompanyLogo = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(imageQueueCompanyLogo, ^
{
UIImage *imageCompanyLogo = [UIImage imageWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:[currentExibitor100 companyLogoURL]]]];
dispatch_async(dispatch_get_main_queue(), ^
{
self.exibitorIcoImageView.image = imageCompanyLogo;
exiIco = imageCompanyLogo;
NSLog(@"qqqqqqqqqqq %@", exiIco);
});
});
return exiIco;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIImage *a = [self ComLog];
NSLog(@"It should be a image %@", a);
}
这里所有的属性都是全局声明的(在“Myclass.h”文件中)。我是目标C的新手。如果你知道答案,请给出答复。
提前感谢。首先,我建议您阅读Objective C中的块。您在函数中使用的
dispatch\u async
块是异步的,因此它在使用后立即返回,因为它在自己的池中运行。为了正确使用,您可以调用另一个方法返回块中的映像进程,或在图像准备就绪时发布通知。如下所示:
-(void) ComLog
{
ExibitorInfo *currentExibitor100 = [[ExibitorInfo alloc] init];
currentExibitor100 = [self.exibitorsArray objectAtIndex:0];
imageQueueCompanyLogo = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(imageQueueCompanyLogo, ^
{
UIImage *imageCompanyLogo = [UIImage imageWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:[currentExibitor100 companyLogoURL]]]];
dispatch_async(dispatch_get_main_queue(), ^
{
self.exibitorIcoImageView.image = imageCompanyLogo;
exiIco = imageCompanyLogo;
NSLog(@"qqqqqqqqqqq %@", exiIco);
[self imageIsReady:exiIco];
});
});
// return exiIco;
}
- (void)imageIsReady:(uiimage *)image
{
//do whatever you want with the image
NSLog(@"Image is here %@", image);
}
代码片段中有太多错误,很难决定从哪里开始 我建议暂时离开GCD,等你更有经验的时候再看一看 基本上,您希望从远程服务器加载映像。
NSURLConnection
提供了一种方便的方法,对于非常简单的用例来说就足够了:
+(void)sendAsynchronousRequest:(NSURLRequest*)请求队列:(NSOperationQueue*)队列完成处理程序:(void(^)(NSURLResponse*,NSData*,NSError*))处理程序;
您可以在此处找到文档:
加载远程资源的推荐方法是在异步模式下使用NSURLConnection
实现委托。您可以在此处找到更多信息:
我也建议你阅读
下面是一个如何使用sendAsynchronousRequest的简短示例:
NSURL* url = [NSURL URLWithString:[currentExibitor100 companyLogoURL]];
NSMutableURLRequest* urlRequest = [NSURLRequest requestWithURL:url];
NSOperationQueue* queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:urlRequest
queue:queue
completionHandler:^(NSURLResponse* response,
NSData* data,
NSError* error)
{
if (data) {
// check status code, and optionally MIME type
if ( [(NSHTTPURLResponse*)(response) statusCode] == 200 /* OK */) {
UIImage* image = [UIImage imageWithData:data];
if (image) {
dispatch_async(dispatch_get_main_queue(), ^{
self.exibitorIcoImageView.image = image;
});
} else {
NSError* err = [NSError errorWithDomain: ...];
[self handleError:err]; // execute on main thread!
}
}
else {
// status code indicates error, or didn't receive type of data requested
NSError* err = [NSError errorWithDomain:...];
[self handleError:err]; // execute on main thread!
}
}
else {
// request failed - error contains info about the failure
[self handleError:error]; // execute on main thread!
}
}];
你的方法在执行块之前返回。看一看,也许会有帮助。我认为这个问题没有帮助,方法会在队列完成之前返回,添加
\u block
不会改变这一点。当队列完成时,你需要使用回调/块将图像类型返回给调用方法。下面是一些示例可能对你有帮助的信息,好的,我会看看这些主题。顺便说一下,谢谢你的评论。:)这被称为“完成处理程序”!!…是的,它可以像你在这里做的一样完成。非常感谢你的回复。@Fahri Azimov:)对不起,什么叫“完成处理程序”?@Bala只是不知道这里发生了什么:)不需要教我什么是完成处理程序和其他事情。我会自己解决。而且,图伦很乐意帮忙。@FahriAzimov这不适合你,你在这里做得很好。哇!你在这里发布了一篇非常机智的帖子。是的,我肯定会在这里阅读你的所有建议。非常感谢你的努力。@CouchDeveloper:)