Ios 使用SDWebImage块在ImageView上显示圆形进度条
我正在尝试使用SDWebImage实现一个类似instagram的圆形进度条,用于图像加载。 1.我正在使用这段代码,但它工作不正常。将图像应用于imageview后,进度条不会隐藏Ios 使用SDWebImage块在ImageView上显示圆形进度条,ios,asynchronous,progress-bar,nsthread,sdwebimage,Ios,Asynchronous,Progress Bar,Nsthread,Sdwebimage,我正在尝试使用SDWebImage实现一个类似instagram的圆形进度条,用于图像加载。 1.我正在使用这段代码,但它工作不正常。将图像应用于imageview后,进度条不会隐藏 单元重用问题也导致了一些问题 还有一个警告正在发出,我正在尝试更新后台线程中的进度条 我怎样才能摆脱这些问题 - (void)setImageInImageView:(UIImageView *)imgv withURLString:(NSString *)str_url { //GuestDefaultPr
- (void)setImageInImageView:(UIImageView *)imgv withURLString:(NSString *)str_url {
//GuestDefaultProfilePic
[imgv sd_setImageWithURL:[NSURL URLWithString:str_url] placeholderImage:[UIImage imageNamed:@"ImageGalleryBackground"] options: SDWebImageHighPriority progress:^(NSInteger receivedSize, NSInteger expectedSize){
NSLog(@"the progress is %f", ((receivedSize*1.0f) * 100) / expectedSize);
progressView = [imgv viewWithTag:4321];
if(progressView==nil)
{
progressView = [[THCircularProgressView alloc] initWithFrame:CGRectMake(imgv.center.x - 20 , imgv.center.y-20 , 60 , 60)];
progressView.lineWidth = 5.0f;
progressView.progressColor = [UIColor whiteColor];
progressView.progressBackgroundColor = [UIColor grayColor];
progressView.centerLabelVisible = YES;
progressView.tag = 4321;
[imgv addSubview:progressView];
}
dispatch_async(dispatch_get_main_queue(), ^{
[progressView setPercentage:(receivedSize*1.0f) / expectedSize];
});
if(receivedSize >= expectedSize)
{
progressView.hidden = YES;
}
}
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL){
[progressView setHidden:YES];
[progressView removeFromSuperview];
[progressView setPercentage:0];
[imgv setImage:image];
}];
}
每次调用进度块时,都会添加一个新的progressView。相反,请执行以下操作:
THCircularProgressView *progressView = [imgv viewWithTag:4321];
if(progressView==nil)
{
progressView = [[THCircularProgressView alloc] initWithFrame:CGRectMake(imgv.center.x - 20 , imgv.center.y-20 , 60 , 60)];
progressView.lineWidth = 5.0f;
progressView.progressColor = [UIColor whiteColor];
progressView.progressBackgroundColor = [UIColor grayColor];
progressView.centerLabelVisible = YES;
progressView.tag = 4321;
[imgv addSubview:progressView];
}
[progressView setPercentage:(receivedSize*1.0f) / expectedSize];
或者,您也可以使用库,或者使用SDWebImage
文档中的库
对于后台线程问题,将进度更新包装在调度块中:
dispatch_async(dispatch_get_main_queue(), ^{
[progressView setPercentage:(receivedSize*1.0f) / expectedSize];
});
检查github上的cocoapod SDWebImage CircularProgressView:
通过这个吊舱你可以实现
- 圆形(使用DACircularProgress)
- 线性的
- 习俗
-(void)nkv_setImageWithURL:(NSURL *)url usingProgressViewType:(ProgressViewType)progressViewType orCustomProgressView:(UIProgressView *)progressView;
Swift 4
mediumImageView.setImageWith(imageUrl, placeholderImage: placeHolder, options: [.progressiveDownload], usingProgressIndicatorWithProgressTintColor: UIColor.red, andTrackTintColor: UIColor.blue))
你可以试试这个
pod“MBCircularProgressBar”
@IBOutlet weak var progressView: MBCircularProgressBarView!
cell.imgAlbum.sd_setImage(with: URL(string: imageUrl), placeholderImage: UIImage(named: "placeholderImage"), options: .highPriority, progress: { (receivedSize :Int, ExpectedSize :Int, url:URL?) in
DispatchQueue.main.async {
cell.ActivityView.isHidden = false;
cell.ActivityView.value = CGFloat(receivedSize)
cell.ActivityView.maxValue = CGFloat(ExpectedSize)
}
}) { (image : UIImage?, error : Error?, cacheType : SDImageCacheType?, url:URL?) in
DispatchQueue.main.async {
UIView.animate(withDuration: 3.0, animations: {() -> Void in
cell.ActivityView.isHidden = true;
})}
cell.imgBlur.removeBlurEffect()
}
我已经用你的代码更新了代码。还编辑了问题,现在进度视图甚至不可见。现在看代码中的问题。这可能是什么问题?虽然这可能从理论上回答了这个问题,但在这里包括答案的基本部分,并提供链接供参考。嗨,@shikhar你有这个问题的解决方案吗?请解释答案。