[英]SDWebImage Class
如何使用此方法在占位符中執行活動指標?
- (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 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];
}];
}
希望這可以幫助
這是UIImageView上的類別,使用SDWebImage下載圖像時會添加進度視圖。
您應該做的是使用UIActivityIndicatorView
更改UIProgressView
嘗試修改如下代碼:
- (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);
}
}
];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.