SDWebImage 类
Posted
技术标签:
【中文标题】SDWebImage 类【英文标题】:SDWebImage Class 【发布时间】:2014-03-07 08:53:04 【问题描述】:如何使用此方法在占位符中执行活动指示器?
- (void)createActivityIndicatorWithStyle:(UIActivityIndicatorViewStyle) activityStyle
if ([self activityIndicator] == nil)
self.activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:activityStyle];
self.activityIndicator.autoresizingMask = UIViewAutoresizingNone;
//calculate the correct position
float width = self.activityIndicator.frame.size.width;
float height = self.activityIndicator.frame.size.height;
float x = (self.frame.size.width / 2.0) - width/2;
float y = (self.frame.size.height / 2.0) - height/2;
self.activityIndicator.frame = CGRectMake(x, y, width, height);
self.activityIndicator.hidesWhenStopped = YES;
[self addSubview:self.activityIndicator];
[self.activityIndicator startAnimating];
感谢任何帮助
【问题讨论】:
你把指标放在哪里?是 UIImageView 吗? 是的,我需要这样做并将指示器放在 UIImageView 占位符中 需要修改SDWebImage的开源,可以接受吗? 是的,我接受,你能帮忙吗? 【参考方案1】:你可以这样做,我在这个例子中使用它来更新单元格,但对于你需要的任何用途都是一样的。
我正在做的是将 UIImageView alpha 设置为 0,添加 UIActivityIndicatore,并在加载图像时调用一个块来获得通知。 加载后,UIImageView alpha 将被设置回 1,并删除 UIActivityIndicator。
-(void)updateCell
self.imageView = [[UIImageView alloc]initWithFrame:frameOfCell];
self.imageView.alpha = 0;
[self addSubview:self.imageView];
//Activity indivator:
self.activity= [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
self.activity.hidesWhenStopped = YES;
self.activity.frame = CGRectMake((frameOfCell.size.width/2)-10, (frameOfCell.size.height/2)-20, 40, 40);
[self.activity startAnimating];
[self addSubview:self.activity];
[self.imageView setContentMode:UIViewContentModeScaleAspectFit];
NSURL *urlOfImage = [NSURL URLWithString:self.imageName];
self.imageView.alpha = 0;
[self.imageView setImageWithURL:urlOfImage completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType)
///
self.imageView.alpha = 1;
//Removing activity indicator:
[self.activity stopAnimating];
[self.activity removeFromSuperview];
];
希望对你有帮助
【讨论】:
【参考方案2】:这里是a Category on UIImageView, adding a progress view while images are downloaded using SDWebImage.
你应该做的是将UIProgressView
更改为UIActivityIndicatorView
尝试像这样修改代码:
- (void)removeIndicatorView
[self.activityIndicator stopAnimating];
- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletedBlock)completedBlock withActivityStyle:(UIActivityIndicatorViewStyle) activityStyle
[self createActivityIndicatorWithStyle:activityStyle];
__weak typeof(self) weakSelf = self;
[self setImageWithURL:url
placeholderImage:placeholder
options:options
progress:^(NSUInteger receivedSize, long long expectedSize)
//If you don't care progress, do nothing
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType)
[weakSelf removeIndicatorView];
if (completedBlock)
completedBlock(image, error, cacheType);
];
【讨论】:
以上是关于SDWebImage 类的主要内容,如果未能解决你的问题,请参考以下文章