首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SDWebImage类

SDWebImage类
EN

Stack Overflow用户
提问于 2014-03-07 16:53:04
回答 2查看 161关注 0票数 1

如何使用此方法在占位符中执行活动指示器?

代码语言:javascript
复制
- (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];

}

感谢您的帮助

EN

回答 2

Stack Overflow用户

发布于 2014-03-07 17:44:46

您可以这样做,我在本例中使用它来更新单元格,但它对于您需要的任何用途都是相同的。

我正在做的是将UIImageView alpha设置为0,添加一个UIActivityIndicatore,并调用一个块以在图像加载了bees时得到通知。加载时,UIImageView alpha将被设置回1,并删除UIActivityIndicator。

代码语言:javascript
复制
 -(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];


}];

}

希望这能有所帮助

票数 1
EN

Stack Overflow用户

发布于 2014-03-07 17:48:27

这是a Category on UIImageView, adding a progress view while images are downloaded using SDWebImage.

您应该做的是使用UIActivityIndicatorView更改UIProgressView

试着像这样修改代码:

代码语言:javascript
复制
- (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);
                    }
                }
     ];
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22245480

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档