SDWebImage progress中使用MBProgress进度条必须回到主线程才能刷新进度,否则进度条不会动。
代码:
//已经封装好的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) {
//加载进度
float progress = (float)receivedSize/expectedSize;
NSLog(@"下载进度:%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:@"没找到高清图" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
[alertView show];
}else{
self.navigationItem.rightBarButtonItem.enabled = YES;
}
}];
网友评论