【问题标题】:Activity indicator in SdWebImage issueSdWebImage 问题中的活动指示器
【发布时间】:2015-03-29 06:46:12
【问题描述】:

我有一个水平集合视图,它有 20 个带有 UIImageView 的单元格 我只想显示活动指示器,直到图像下载完成。我正在使用新的 SDWebImage 库,其中我们有 sd_setImageWithURL 方法 到现在我在做的是

__block UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.center = cell.photoOneImageView.center;
activityIndicator.hidesWhenStopped = YES;

[cell.photoOneImageView sd_setImageWithURL:[NSURL URLWithString:@"https://scontent-sjc.xx.fbcdn.net/hphotos-xpa1/v/t1.0-9/p480x480/11073324_10153728863852926_4010319763478440264_n.jpg?oh=590934059508b7da235a46fc39e08063&oe=55B61458"] 
placeholderImage:[UIImage imageNamed:@"placeholder.jpg"] 
completed:^(UIImage *image, NSError *error,  SDImageCacheType cacheType, NSURL *imageURL) {
    [activityIndicator stopAnimating];
    [activityIndicator removeFromSuperview];
    }];

[cell.photoOneImageView addSubview:activityIndicator];
[activityIndicator startAnimating];

我在 cellForItemAtIndexPath 方法中编写此代码 它有时会在 1 个单元格上显示多个指示器,有时当我们水平滚动集合视图时它们也不会移除。

我看到了这个https://github.com/rs/SDNetworkActivityIndicator,但我无法使用它。没有运气。当图像完成指示器消失时,是否有人在 tebleview 单元格或集合视图单元格中实现了活动指示器。请帮忙。任何帮助将不胜感激。谢谢

【问题讨论】:

  • 您在 cellForRow 中调用 addSubview,而不是将活动指示器作为单元格的一部分。
  • 有没有其他方法...
  • 您可以将活动指示器作为自定义单元格的一部分,就像您不是每次都添加 photoOneImageView 一样......
  • 你的意思是说我应该将它添加为占位符......并保持动画效果。我怎样才能实现它..我在 setimagewithurl 函数中传递了一个 .jpg 。请帮助
  • 它不是占位符,它只是在加载时动画,并在调用完成块时停止动画。您所做的一切只是不要一直在单元格中添加随机指示器:)

标签: ios objective-c uicollectionview uiactivityindicatorview sdwebimage


【解决方案1】:

最好的方法是使用 SDWebImage 在最新 repo 中提供的默认活动指示器

更新:SWIFT 5 SDWebImage 5.x.x

        cell.imageView.sd_imageIndicator = SDWebImageActivityIndicator.gray
        cell.imageView.sd_setImage(with: url) { (image, error, cache, urls) in
            if (error != nil) {
                // Failed to load image
                cell.imageView.image = UIImage(named: "ico_placeholder")
            } else {
                // Successful in loading image
                cell.imageView.image = image
            }
        }

迅速 3:

cell.imageView.setShowActivityIndicator(true)
cell.imageView.setIndicatorStyle(.gray)
cell.imageView?.sd_setImage(with: url) { (image, error, cache, urls) in
                if (error != nil) {
                    // Failed to load image
                    cell.imageView.image = UIImage(named: "ico_placeholder")
                } else {
                    // Successful in loading image
                    cell.imageView.image = image
                }
 }

【讨论】:

    【解决方案2】:

    您遇到的问题是您在一个单元格上添加了一个UIActivityIndicatorView,该单元格可能会在您的块触发之前被取消队列并在稍后重用。

    要解决此问题,请将活动指示器设为单元格的属性:

    @interface Cell : UICollectionViewCell
    
    @property (strong, nonatomic) UIActivityIndicatorView activityIndicator;
    
    @end
    

    然后,您的实现应该如下所示:

    @implementation Cell
    
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            [self initialize];
        }
        return self;
    }
    
    - (void)awakeFromNib
    {
        [super awakeFromNib];
        [self initialize];
    }
    
    - (void)initialize
    {
        // This code is only called once
        self.activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        self.activityIndicator.center = self.photoOneImageView.center;
        self.activityIndicator.hidesWhenStopped = YES;
        [self.photoOneImageView addSubview:self.activityIndicator];
    }
    
    @end
    

    然后:

    [cell.photoOneImageView sd_setImageWithURL:[NSURL URLWithString:@"https://somesite.com/pic.jpg"] 
                              placeholderImage:[UIImage imageNamed:@"placeholder.jpg"] 
                                     completed:
        ^(UIImage *image, NSError *error,  SDImageCacheType cacheType, NSURL *imageURL) {
            [cell.activityIndicator stopAnimating];
        }];
    
    [cell.activityIndicator startAnimating];
    

    【讨论】:

      【解决方案3】:

      您可以使用库 https://github.com/JJSaccolo/UIActivityIndicator-for-SDWebImage 来解决此问题。

      使用活动指示器加载图像非常简单

      [cell.photoOneImageView setImageWithURL:[NSURL URLWithString:@"https://scontent-sjc.xx.fbcdn.net/hphotos-xpa1/v/t1.0-9/p480x480/11073324_10153728863852926_4010319763478440264_n.jpg?oh=590934059508b7da235a46fc39e08063&oe=55B61458"] 
              placeholderImage:[UIImage imageNamed:@"placeholder.jpg"]
          usingActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
      

      【讨论】:

        【解决方案4】:

        迅速 3 对于扩展 UIImageView+Download+Extension.swift:

        import SDWebImage
        
        extension UIImageView {
            func setImageWithIndicator(imageUrl: String, callback: ((_ image: UIImage) -> Void)? = nil) {
                guard !imageUrl.isEmpty else {
                    return
            }
        
                if let url = NSURL(string: imageUrl), url.host != nil {
                    self.setShowActivityIndicator(true)
                    self.setIndicatorStyle(.gray)
                    self.sd_setImage(with: url as URL!, completed: { (_, _, _, _) in
                })
             }
           }
        }
        

        使用:

        UIImageView.setImageWithIndicator(imageUrl: url)
        

        【讨论】:

          【解决方案5】:

          您必须创建一个单独的 CollectionView 单元格才能重复使用。

          或者在创建collectionviewcell之后这样做:

            for (__strong UIView *view in Cell.contentView.subviews) {
          if([view isKindofClass: [UIActivityIndicatorView class]]])
          {
          
                  [view removeFromSuperview];
                  view = nil;}
              }
          

          因为你总是在创建activityindicatorView的新对象

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-04-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-10-23
            • 2016-05-23
            相关资源
            最近更新 更多