SdWebImage问题中的活动指示器

时间:2015-03-29 06:46:13

标签: ios objective-c uicollectionview uiactivityindicatorview sdwebimage

我有一个水平集合视图,它有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单元格或集合视图单元格中实现了活动指示符。请帮忙。任何帮助将不胜感激。感谢

5 个答案:

答案 0 :(得分:4)

最好的方法是使用最新仓库中提供的SDWebImage默认活动指标

迅捷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
                }
 }

答案 1 :(得分: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];

答案 2 :(得分:0)

您可以使用库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];

答案 3 :(得分:0)

迅速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)

答案 4 :(得分:0)

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

或者在创建collectionviewcell后执行此操作:

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

        [view removeFromSuperview];
        view = nil;}
    }

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