如何从GCD返回UIImage类型

时间:2013-08-20 06:30:14

标签: ios objective-c uiimageview uiimage grand-central-dispatch

我有一个名为“ComLog”的“UIImage”返回类型方法。我想从这个方法返回一个Image。在“ComLog”方法中,我使用GCD从数组中获取图像值。我使用下面的代码,“NSLog(@”qqqqqqqqqqq%@“,exiIco)”打印'图像'值但NSLog(@“qqqqqqqqqqq%@”,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的新手。如果你知道答案,请回答。 在此先感谢。

2 个答案:

答案 0 :(得分:2)

您的代码段中存在很多错误,因此很难决定从哪里开始。

我建议暂时离开GCD,稍后当你更有经验时再看一下。

基本上,您希望从远程服务器加载图像。 NSURLConnection为此提供了一个方便的方法,这对于非常简单的用例就足够了:

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

您可以在此处找到文档:NSURLConnection Class Reference

加载远程资源的推荐方法是在实现委托的异步模式下使用NSURLConnection。你可以在这里找到更多信息: URL Loading System Programming Guide - Using NSURL Connection

我还建议您阅读Conventions

以下是如何使用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!
    }        
}];

答案 1 :(得分:0)

首先,我建议你阅读目标C中的块。你在函数中使用的dispatch_async块是异步的,因此它在你使用之后立即返回,因为它在它自己运行池。为了正确使用,您可以调用另一种方法来返回块内的图像处理,或者在图像准备好后发布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);
}