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;
17
14
18
15
@implementation UIImageView (WebCache)
19
16
@@ -50,19 +47,13 @@ - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder
50
47
self.image = placeholder;
51
48
});
52
49
}
53
-
54
- // check if activityView is enabled or not
55
- if ([self showActivityIndicatorView ]) {
56
- [self addActivityIndicator ];
57
- }
58
-
50
+
59
51
if (url) {
60
52
__weak UIImageView *wself = self;
61
53
id <SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadImageWithURL: url options: options progress: progressBlock completed: ^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
62
54
if (!wself) return ;
63
55
dispatch_main_sync_safe (^{
64
56
if (!wself) return ;
65
- [wself removeActivityIndicator ];
66
57
if (image) {
67
58
wself.image = image;
68
59
[wself setNeedsLayout ];
@@ -80,7 +71,6 @@ - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder
80
71
[self sd_setImageLoadOperation: operation forKey: @" UIImageViewImageLoad" ];
81
72
} else {
82
73
dispatch_main_async_safe (^{
83
- [self removeActivityIndicator ];
84
74
NSError *error = [NSError errorWithDomain: @" SDWebImageErrorDomain" code: -1 userInfo: @{NSLocalizedDescriptionKey : @" Trying to load a nil url" }];
85
75
if (completedBlock) {
86
76
completedBlock (nil , error, SDImageCacheTypeNone, url);
@@ -92,8 +82,8 @@ - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder
92
82
- (void )sd_setImageWithPreviousCachedImageWithURL : (NSURL *)url andPlaceholderImage : (UIImage *)placeholder options : (SDWebImageOptions)options progress : (SDWebImageDownloaderProgressBlock)progressBlock completed : (SDWebImageCompletionBlock)completedBlock {
93
83
NSString *key = [[SDWebImageManager sharedManager ] cacheKeyForURL: url];
94
84
UIImage *lastPreviousCachedImage = [[SDImageCache sharedImageCache ] imageFromDiskCacheForKey: key];
95
-
96
- [self sd_setImageWithURL: url placeholderImage: lastPreviousCachedImage ?: placeholder options: options progress: progressBlock completed: completedBlock];
85
+
86
+ [self sd_setImageWithURL: url placeholderImage: lastPreviousCachedImage ?: placeholder options: options progress: progressBlock completed: completedBlock];
97
87
}
98
88
99
89
- (NSURL *)sd_imageURL {
@@ -139,60 +129,4 @@ - (void)sd_cancelCurrentAnimationImagesLoad {
139
129
[self sd_cancelImageLoadOperationWithKey: @" UIImageViewAnimationImages" ];
140
130
}
141
131
142
- - (UIActivityIndicatorView *)activityIndicator {
143
- return (UIActivityIndicatorView *)objc_getAssociatedObject (self, &TAG_ACTIVITY_INDICATOR);
144
- }
145
- - (void )setActivityIndicator : (UIActivityIndicatorView *)activityIndicator {
146
- objc_setAssociatedObject (self, &TAG_ACTIVITY_INDICATOR, activityIndicator, OBJC_ASSOCIATION_RETAIN );
147
- }
148
-
149
- - (void )setShowActivityIndicatorView : (BOOL )show {
150
- objc_setAssociatedObject (self, &TAG_ACTIVITY_SHOW, [NSNumber numberWithBool: show], OBJC_ASSOCIATION_RETAIN );
151
- }
152
- - (BOOL )showActivityIndicatorView {
153
- return [objc_getAssociatedObject (self , &TAG_ACTIVITY_SHOW) boolValue ];
154
- }
155
-
156
- - (void )setIndicatorStyle : (UIActivityIndicatorViewStyle)style {
157
- objc_setAssociatedObject (self, &TAG_ACTIVITY_STYLE, [NSNumber numberWithInt: style], OBJC_ASSOCIATION_RETAIN );
158
- }
159
- - (int )getIndicatorStyle {
160
- return [objc_getAssociatedObject (self , &TAG_ACTIVITY_STYLE) intValue ];
161
- }
162
-
163
- - (void )addActivityIndicator {
164
- if (!self.activityIndicator ) {
165
- self.activityIndicator = [[UIActivityIndicatorView alloc ] initWithActivityIndicatorStyle: [self getIndicatorStyle ]];
166
- self.activityIndicator .autoresizingMask = UIViewAutoresizingNone;
167
-
168
- dispatch_async (dispatch_get_main_queue (), ^(void ) {
169
- [self addSubview: self .activityIndicator];
170
- [self updateActivityIndicatorFrame ];
171
- });
172
- }
173
-
174
- dispatch_async (dispatch_get_main_queue (), ^(void ) {
175
- [self .activityIndicator startAnimating ];
176
- });
177
-
178
- }
179
- - (void )updateActivityIndicatorFrame {
180
- if (self.activityIndicator ) {
181
- CGRect activityIndicatorBounds = self.activityIndicator .bounds ;
182
- float x = (self.frame .size .width - activityIndicatorBounds.size .width ) / 2.0 ;
183
- float y = (self.frame .size .height - activityIndicatorBounds.size .height ) / 2.0 ;
184
- self.activityIndicator .frame = CGRectMake (x, y, activityIndicatorBounds.size .width , activityIndicatorBounds.size .height );
185
- }
186
- }
187
- - (void )removeActivityIndicator {
188
- if (self.activityIndicator ) {
189
- [self .activityIndicator removeFromSuperview ];
190
- self.activityIndicator = nil ;
191
- }
192
- }
193
- - (void )layoutSubviews {
194
- [super layoutSubviews ];
195
- [self updateActivityIndicatorFrame ];
196
- }
197
-
198
132
@end
0 commit comments