延迟加载集合视图与活动指示器

时间:2015-04-18 12:38:47

标签: ios uiimage uicollectionview uiactivityindicatorview

我的问题是我在加载图片后得到了活动指示,但它应该在加载图片之前显示。我使用SDWebImageProgressivedownload代码但是活动指示符在图像显示后加载。请帮助我

__block UIActivityIndicatorView *activityIndicator =    [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:activityStyle];
activityIndicator.center = imageView.center;
activityIndicator.hidesWhenStopped = YES;
[imageView setImageWithURL:[NSURL  URLWithString:@"http://www.domain.com/path/to/image.jpg"]
           placeholderImage:[UIImage imageNamed:@"placeholder.png"]
                    success:^(UIImage *image) { [activityIndicator removeFromSuperview]; }
failure:^(NSError *error) { [activityIndicator removeFromSuperview]; }];

[imageView addSubview:activityIndicator];
[activityIndicator startAnimating];

2 个答案:

答案 0 :(得分:1)

您遇到的问题是您在一个单元格上添加了一个UIActivityIndi​​catorView,该单元格可能会在您的块触发之前被取消并重新使用。

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

@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

然后在你的cellForItemAtIndex方法

[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];

我想它应该有用

答案 1 :(得分:0)

SDWebImage推荐adding a progress indicator

此类别 祝你好运