Ios 嵌套GCD块中的nil值

Ios 嵌套GCD块中的nil值,ios,objective-c,uiimage,grand-central-dispatch,Ios,Objective C,Uiimage,Grand Central Dispatch,我有一个集合视图,需要显示其文件保存在设备上的图像集合。它们都是600x600像素,因此我认为最好在后台线程上异步创建UIImage,然后在主线程上设置myUIImageView的image属性。下面是我在(UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath方法中执行此操作的地方 dispatch_async(di

我有一个集合视图,需要显示其文件保存在设备上的图像集合。它们都是600x600像素,因此我认为最好在后台线程上异步创建UIImage,然后在主线程上设置my
UIImageView
image
属性。下面是我在
(UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath方法中执行此操作的地方

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{

        NSString *imageName = [NSString stringWithFormat:@"wo-%@", template.fileName];

        UIImage *image = [UIImage imageNamed:imageName];
        dispatch_async(dispatch_get_main_queue(), ^{
            if (image == nil)
            {
                self.collectionView.backgroundColor = [UIColor redColor];
                NSLog(@"image named: %@ in nil!", imageName);
            }
            cell.imageView.image = image;
        });
    });

    return cell;

每隔一段时间,其中一个单元格的
图像
将为
nil
。它是不可复制的,并且不总是相同的单元格/图像名称。我想知道这是否与使用
UIImage imageNamed:
方法有关,还是我使用GCD不正确?我很感兴趣的是为什么
image
会偶尔变成
nil
,而不一定是做同样事情的替代方法。

使用
\u block
关键字您可以使用
SDWebimage
库从链接加载图像anynchorsly@Dhiru,你能解释一下为什么吗?它是零,因为可能图像还没有下载完毕,而你已经在调用它了?