ImageView上的循环进度条使用SDWebImage块

时间:2015-10-27 14:22:11

标签: ios asynchronous progress-bar nsthread sdwebimage

我正在尝试使用SDWebImage实现类似于循环进度条的Instagram以进行图像加载。 我正在使用这段代码,但它无法正常工作。将图像应用于图像视图后,进度条不会被隐藏。

  1. 细胞再利用问题也引发了一些问题。
  2. 还发出警告,我正在尝试更新后台线程中的进度条。
  3. 如何摆脱这些问题

    - (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];
    
        }];
    
    
    }
    

4 个答案:

答案 0 :(得分:2)

你可以试试这个,

  1. 安装pod "MBCircularProgressBar"
  2. 将UIView作为您想要进度视图的自定义单元格并设置类MBCircularProgressBarView。

    @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()
    }
    

答案 1 :(得分:0)

每次调用进度块时都会添加一个新的progressView。相反,这样做:

I = imread('download.bmp');
imshow(I);title('Input Image');

imhist(I(:));title('Histogram of input image');

或者,您可以在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]; 文档中使用this库或this one {/ 3}}。

对于后台线程问题,请将进度更新包装在一个调度块中:

SDWebImage

答案 2 :(得分:0)

在github上检查cocoapod SDWebImage-CircularProgressView:
https://github.com/NikKovV/SDWebImage-CircularProgressView

通过此窗格,您可以实现

  • 循环(使用DACircularProgress)
  • 线性
  • 定制

ProgressViews。

这是UIImageView的一个类别。

例如

-(void)nkv_setImageWithURL:(NSURL *)url usingProgressViewType:(ProgressViewType)progressViewType orCustomProgressView:(UIProgressView *)progressView;

答案 3 :(得分:0)

Swift 4

mediumImageView.setImageWith(imageUrl, placeholderImage: placeHolder, options: [.progressiveDownload], usingProgressIndicatorWithProgressTintColor: UIColor.red, andTrackTintColor: UIColor.blue))