如何从GCD返回UIImage类型 [英] How to return an UIImage type from a GCD

查看:59
本文介绍了如何从GCD返回UIImage类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为"ComLog"的"UIImage"返回类型方法.我想从该方法返回一个图像.在"ComLog"方法中,我使用GCD从数组中获取图像值.我使用以下代码,"NSLog(@" qqqqqqqqqqq%@,exiIco)"打印'image'值,但NSLog(@"qqqqqqqqqqq%@",exiIco);不. 这是详细信息:

I have an "UIImage" return type method named "ComLog". I want to return a Image from this method. In "ComLog" method i use GCD to get the image value from an array. I use the following code, the "NSLog(@"qqqqqqqqqqq %@", exiIco)" print the 'image' value but NSLog(@"qqqqqqqqqqq %@", exiIco);" don't. Here is the details :

-(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的新手.如果您知道答案,请给予答复. 预先感谢.

Here all the properties are declared globally(In "Myclass.h" file). I am new in Objective C. Please give reply if you know the answer. Thanks in Advance.

推荐答案

首先,我建议您阅读一下Objective C中的块.在函数内部使用的dispatch_async块是异步的,因此它返回使用它后,它会立即在它自己的池中运行.为了正确使用,您可以调用另一种方法来返回块内的图像处理,或者在图像准备好后发布NSNotification.像这样:

First of all, I would recommend you to read about blocks in Objective C. The dispatch_async block you are using inside your function is async and thus it returns immediately after you use it, as it runs in it's own pool. For proper use, you can call another method to return the image processes inside the block, or post NSNotification when your image is ready. like this:

-(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返回UIImage类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆