Skip to content

Commit c68d585

Browse files
committed
activity indicator added
``` [cell.imageView setShowActivityIndicatorView:YES]; [cell.imageView setIndicatorStyle:UIActivityIndicatorViewStyleGray]; ```
1 parent 4cfb12c commit c68d585

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

Examples/SDWebImage Demo/MasterViewController.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,9 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
379379
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
380380
}
381381

382+
[cell.imageView setShowActivityIndicatorView:YES];
383+
[cell.imageView setIndicatorStyle:UIActivityIndicatorViewStyleGray];
384+
382385
cell.textLabel.text = [NSString stringWithFormat:@"Image #%ld", (long)indexPath.row];
383386
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
384387
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:[_objects objectAtIndex:indexPath.row]]

SDWebImage/UIImageView+WebCache.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,18 @@
176176

177177
- (void)sd_cancelCurrentAnimationImagesLoad;
178178

179+
/**
180+
* Show activity UIActivityIndicatorView
181+
*/
182+
- (void)setShowActivityIndicatorView:(BOOL)show;
183+
184+
/**
185+
* set desired UIActivityIndicatorViewStyle
186+
*
187+
* @param style The style of the UIActivityIndicatorView
188+
*/
189+
- (void)setIndicatorStyle:(UIActivityIndicatorViewStyle)style;
190+
179191
@end
180192

181193

SDWebImage/UIImageView+WebCache.m

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
#import "UIView+WebCacheOperation.h"
1212

1313
static char imageURLKey;
14+
static char TAG_ACTIVITY_INDICATOR;
15+
static char TAG_ACTIVITY_STYLE;
16+
static char TAG_ACTIVITY_SHOW;
1417

1518
@implementation UIImageView (WebCache)
1619

@@ -49,8 +52,15 @@ - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder
4952
}
5053

5154
if (url) {
55+
56+
// check if activityView is enabled or not
57+
if ([self showActivityIndicatorView]) {
58+
[self addActivityIndicator];
59+
}
60+
5261
__weak __typeof(self)wself = self;
5362
id <SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadImageWithURL:url options:options progress:progressBlock completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
63+
[wself removeActivityIndicator];
5464
if (!wself) return;
5565
dispatch_main_sync_safe(^{
5666
if (!wself) return;
@@ -76,6 +86,7 @@ - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder
7686
[self sd_setImageLoadOperation:operation forKey:@"UIImageViewImageLoad"];
7787
} else {
7888
dispatch_main_async_safe(^{
89+
[self removeActivityIndicator];
7990
NSError *error = [NSError errorWithDomain:SDWebImageErrorDomain code:-1 userInfo:@{NSLocalizedDescriptionKey : @"Trying to load a nil url"}];
8091
if (completedBlock) {
8192
completedBlock(nil, error, SDImageCacheTypeNone, url);
@@ -134,6 +145,70 @@ - (void)sd_cancelCurrentAnimationImagesLoad {
134145
[self sd_cancelImageLoadOperationWithKey:@"UIImageViewAnimationImages"];
135146
}
136147

148+
149+
#pragma mark -
150+
- (UIActivityIndicatorView *)activityIndicator {
151+
return (UIActivityIndicatorView *)objc_getAssociatedObject(self, &TAG_ACTIVITY_INDICATOR);
152+
}
153+
154+
- (void)setActivityIndicator:(UIActivityIndicatorView *)activityIndicator {
155+
objc_setAssociatedObject(self, &TAG_ACTIVITY_INDICATOR, activityIndicator, OBJC_ASSOCIATION_RETAIN);
156+
}
157+
158+
- (void)setShowActivityIndicatorView:(BOOL)show{
159+
objc_setAssociatedObject(self, &TAG_ACTIVITY_SHOW, [NSNumber numberWithBool:show], OBJC_ASSOCIATION_RETAIN);
160+
}
161+
162+
- (BOOL)showActivityIndicatorView{
163+
return [objc_getAssociatedObject(self, &TAG_ACTIVITY_SHOW) boolValue];
164+
}
165+
166+
- (void)setIndicatorStyle:(UIActivityIndicatorViewStyle)style{
167+
objc_setAssociatedObject(self, &TAG_ACTIVITY_STYLE, [NSNumber numberWithInt:style], OBJC_ASSOCIATION_RETAIN);
168+
}
169+
170+
- (int)getIndicatorStyle{
171+
return [objc_getAssociatedObject(self, &TAG_ACTIVITY_STYLE) intValue];
172+
}
173+
174+
- (void)addActivityIndicator {
175+
if (!self.activityIndicator) {
176+
self.activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:[self getIndicatorStyle]];
177+
self.activityIndicator.translatesAutoresizingMaskIntoConstraints = NO;
178+
179+
dispatch_main_async_safe(^{
180+
[self addSubview:self.activityIndicator];
181+
182+
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.activityIndicator
183+
attribute:NSLayoutAttributeCenterX
184+
relatedBy:NSLayoutRelationEqual
185+
toItem:self
186+
attribute:NSLayoutAttributeCenterX
187+
multiplier:1.0
188+
constant:0.0]];
189+
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.activityIndicator
190+
attribute:NSLayoutAttributeCenterY
191+
relatedBy:NSLayoutRelationEqual
192+
toItem:self
193+
attribute:NSLayoutAttributeCenterY
194+
multiplier:1.0
195+
constant:0.0]];
196+
});
197+
}
198+
199+
dispatch_main_async_safe(^{
200+
[self.activityIndicator startAnimating];
201+
});
202+
203+
}
204+
205+
- (void)removeActivityIndicator {
206+
if (self.activityIndicator) {
207+
[self.activityIndicator removeFromSuperview];
208+
self.activityIndicator = nil;
209+
}
210+
}
211+
137212
@end
138213

139214

0 commit comments

Comments
 (0)