ios - 如何从 GCD 返回 UIImage 类型

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

我有一个名为“ComLog”的“UIImage”返回类型方法。我想从此方法返回一个图像。在“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”文件中)。我是Objective C的新手,知道的请回复。 提前致谢。

最佳答案

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

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

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

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

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

加载远程资源的推荐方法是在异步模式下使用 NSURLConnection 实现委托(delegate)。您可以在此处找到更多信息: 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!
    }        
}];

关于ios - 如何从 GCD 返回 UIImage 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18328379/

相关文章:

ios - 初始化单例异步 iOS

ios - 如何从 Swift 2 中的字符串中删除特殊字符?

ios - 如何正确补偿CGContextRef坐标系,使原点在左上角

ios - 异步加载内容时如何设置UIScrollView contensize

ios - 自定义字体仅在 Interface Builder 中设置时可用

ios - 从 mapView 中删除一条折线

ios - 单击时删除 UITableViewCells 按钮

iphone - 保存并加载 UISwitch 状态

iphone - 无法移动我的角色

iphone - 仪器优化 UIImageView