问题描述:使用SDWebImageDownloader 将图片数组全部下载由于异步操作,图片没下载完全导致传入的数组没有数据。
处理方法:
dispatch_group_t group = dispatch_group_create();
for (NSString *urlStr in arr) {
dispatch_group_enter(group);
[[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:urlStr] options:(SDWebImageDownloaderHighPriority) progress:^(NSInteger receivedSize, NSInteger expectedSize) {
} completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
if (image) {
[self.photoArr addObject:image];
}else{
UIImage *imagePlaceholder = [UIImage imageNamed:@"加载失败"];
[self.photoArr addObject:imagePlaceholder];
}
dispatch_group_leave(group);
}];
}
// 下载图片完成后, 回到主线
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
// 刷新UI
self.feedPhotoView.photoArr = self.photoArr;
});