Sdwebimage異步下載多張圖片
dispatch_group_t group = dispatch_group_create();
// 有多張圖片URL的數(shù)組
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
});
和內(nèi)存管理的引用計數(shù)類似妻熊,我們可以認為group也持有一個整形變量(只是假設(shè))夸浅,
當(dāng)調(diào)用enter時計數(shù)加1,調(diào)用leave時計數(shù)減1固耘,
當(dāng)計數(shù)為0時會調(diào)用dispatch_group_notify并且dispatch_group_wait會停止等待题篷;
詳細dispatch_group_t group 參考:
http://www.reibang.com/p/228403206664