0

我有一个名为“ComLog”的“UIImage”返回类型方法。我想从这个方法返回一个图像。在“ComLog”方法中,我使用 GCD 从数组中获取图像值。我使用以下代码,"NSLog(@"qqqqqqqqqq %@", exiIco)" 打印 'image' 值,但 NSLog(@"qqqqqqqqqq %@", 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”文件中)。我是Objective C的新手。如果您知道答案,请回复。提前致谢。

4

2 回答 2

2

您的代码片段中有很多错误,以至于很难决定从哪里开始。

我建议暂时离开GCD,等你有经验的时候再看看。

基本上,您想从远程服务器加载图像。NSURLConnection为此提供了一种方便的方法,足以满足非常简单的用例:

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

您可以在此处找到文档:NSURLConnection 类参考

加载远程资源的推荐方法是NSURLConnection在异步模式下使用实现委托。您可以在此处找到更多信息: URL 加载系统编程指南 - 使用 NSURL 连接

我还建议阅读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!
    }        
}];
于 2013-08-20T07:27:00.103 回答
0

首先,我建议您阅读 Objective 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);
}
于 2013-08-20T06:53:05.540 回答