如何从GCD返回UIImagetypes

我有一个名为“ComLog”的“UIImage”返回types的方法。 我想从这个方法返回一个图像。 在“ComLog”方法中,我使用GCD从数组中获取图像值。 我使用下面的代码,“NSLog(@”qqqqqqqqqqq%@“,exiIco)”打印'图像'的值,但NSLog(@“qqqqqqqqqqqq%@”,exiIco);“不要这样的细节:

-(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中的新人。如果你知道答案,请回答。 提前致谢。

首先,我build议您阅读Objective C中的块。您在函数中使用的dispatch_async块是asynchronous的,因此在使用它之后会立即返回,因为它在自己的池中运行。 为了正确使用,您可以调用其他方法来返回块内的image processing,或者在图像准备就绪后发布NSNotification 。 喜欢这个:

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

代码片段中有太多错误,很难决定从哪里开始。

我build议现在就离开GCD,稍后当你更有经验的时候再去看看。

基本上,你想从远程服务器加载图像。 NSURLConnection提供了一个方便的方法,这对于非常简单的用例来说是足够的:

+ (void)sendAsynchronousRequest:(NSURLRequest *)request queue:(NSOperationQueue *)queue completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))handler;

你可以在这里find文档: NSURLConnection类参考 。

推荐的加载远程资源的方法是使用NSURLConnection以asynchronous模式实现委托。 您可以在这里find更多信息: URL加载系统编程指南 – 使用NSURL连接

我也build议阅读公约 。

这里是一个简单的例子,如何使用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! } }];