SDWebImage progress中使用MBProgress進(jìn)度條必須回到主線程才能刷新進(jìn)度,否則進(jìn)度條不會(huì)動(dòng)蜂挪。
代碼:
//已經(jīng)封裝好的MBProgressHUDProgress
_HUD = [Tools MBProgressHUDProgress:@"Loading"];
_HUD.progress = 0;
//SDWebImage加載圖片
[_imageView sd_setImageWithURL:wallpaper.fullSize placeholderImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:wallpaper.thumbnail]] options:SDWebImageCacheMemoryOnly progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
//加載進(jìn)度
float progress = (float)receivedSize/expectedSize;
NSLog(@"下載進(jìn)度:%f",progress);
dispatch_sync(dispatch_get_main_queue() , ^{
//必須返回主線程才能刷新UI
_HUD.progress = progress;
});
} completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
[_HUD hideAnimated:YES];
if (error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"沒(méi)找到高清圖" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
[alertView show];
}else{
self.navigationItem.rightBarButtonItem.enabled = YES;
}
}];