iOS批量图像下载完成和回调

时间:2017-04-22 05:40:31

标签: ios sdwebimage

我有一个批量图片网址,我希望在所有图片下载完成后加载自定义块。

我使用SDWebImage,下载图像功能是异步的,所以我该怎么办?

1 个答案:

答案 0 :(得分:0)

您可以使用dispatch_group_t执行此操作:

dispatch_group_t group =  dispatch_group_create();
for (NSURL *url in urls) {
    dispatch_group_enter(group);
    [[[SDWebImageManager sharedManager] imageDownloader] downloadImageWithURL:url options:SDWebImageDownloaderLowPriority progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
        if (image) {
            // download success
        }
        NSLog(@"download...");
        dispatch_group_leave(group);
    }];
}

dispatch_group_notify(group, dispatch_get_main_queue(), ^{
    NSLog(@"all download...");
});