Sdwebimage异步下载多张图片
dispatch_group_t group = dispatch_group_create();
// 有多张图片URL的数组
NSArray *imageURLArr = @[@"url",@"url",@"url"];
for (NSString *imageUrlStr in imageURLArr) {
dispatch_group_enter(group);
// 需要加载图片的控件(UIImageView, UIButton等)
UIImageView *imageView = [UIImageView new];
[imageView sd_setImageWithURL:[NSURL URLWithString:imageUrlStr] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
if (error) {
// 加载失败
} else {
// 加载成功
}
dispatch_group_leave(group);
}];
}
// 下载图片完成后, 回到主线
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
// 刷新UI
});
和内存管理的引用计数类似,我们可以认为group也持有一个整形变量(只是假设),
当调用enter时计数加1,调用leave时计数减1,
当计数为0时会调用dispatch_group_notify并且dispatch_group_wait会停止等待;
详细dispatch_group_t group 参考:
https://www.jianshu.com/p/228403206664