11
11
#import " UIView+WebCacheOperation.h"
12
12
13
13
static char imageURLKey;
14
+ static char TAG_ACTIVITY_INDICATOR;
15
+ static char TAG_ACTIVITY_STYLE;
16
+ static char TAG_ACTIVITY_SHOW;
14
17
15
18
@implementation UIImageView (WebCache)
16
19
@@ -49,8 +52,15 @@ - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder
49
52
}
50
53
51
54
if (url) {
55
+
56
+ // check if activityView is enabled or not
57
+ if ([self showActivityIndicatorView ]) {
58
+ [self addActivityIndicator ];
59
+ }
60
+
52
61
__weak __typeof (self)wself = self;
53
62
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 ];
54
64
if (!wself) return ;
55
65
dispatch_main_sync_safe (^{
56
66
if (!wself) return ;
@@ -76,6 +86,7 @@ - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder
76
86
[self sd_setImageLoadOperation: operation forKey: @" UIImageViewImageLoad" ];
77
87
} else {
78
88
dispatch_main_async_safe (^{
89
+ [self removeActivityIndicator ];
79
90
NSError *error = [NSError errorWithDomain: SDWebImageErrorDomain code: -1 userInfo: @{NSLocalizedDescriptionKey : @" Trying to load a nil url" }];
80
91
if (completedBlock) {
81
92
completedBlock (nil , error, SDImageCacheTypeNone, url);
@@ -134,6 +145,70 @@ - (void)sd_cancelCurrentAnimationImagesLoad {
134
145
[self sd_cancelImageLoadOperationWithKey: @" UIImageViewAnimationImages" ];
135
146
}
136
147
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
+
137
212
@end
138
213
139
214
0 commit comments