Authored by Whirlwind

Refactor the cancel logic

@@ -193,6 +193,12 @@ @@ -193,6 +193,12 @@
193 A18A6CC8172DC28500419892 /* UIImage+GIF.h in Headers */ = {isa = PBXBuildFile; fileRef = A18A6CC5172DC28500419892 /* UIImage+GIF.h */; }; 193 A18A6CC8172DC28500419892 /* UIImage+GIF.h in Headers */ = {isa = PBXBuildFile; fileRef = A18A6CC5172DC28500419892 /* UIImage+GIF.h */; };
194 A18A6CC9172DC28500419892 /* UIImage+GIF.m in Sources */ = {isa = PBXBuildFile; fileRef = A18A6CC6172DC28500419892 /* UIImage+GIF.m */; }; 194 A18A6CC9172DC28500419892 /* UIImage+GIF.m in Sources */ = {isa = PBXBuildFile; fileRef = A18A6CC6172DC28500419892 /* UIImage+GIF.m */; };
195 A18A6CCA172DC28500419892 /* UIImage+GIF.m in Sources */ = {isa = PBXBuildFile; fileRef = A18A6CC6172DC28500419892 /* UIImage+GIF.m */; }; 195 A18A6CCA172DC28500419892 /* UIImage+GIF.m in Sources */ = {isa = PBXBuildFile; fileRef = A18A6CC6172DC28500419892 /* UIImage+GIF.m */; };
  196 + AB615303192DA24600A2D8E9 /* UIView+WebCacheOperation.h in Headers */ = {isa = PBXBuildFile; fileRef = AB615301192DA24600A2D8E9 /* UIView+WebCacheOperation.h */; };
  197 + AB615304192DA24600A2D8E9 /* UIView+WebCacheOperation.h in Headers */ = {isa = PBXBuildFile; fileRef = AB615301192DA24600A2D8E9 /* UIView+WebCacheOperation.h */; };
  198 + AB615305192DA24600A2D8E9 /* UIView+WebCacheOperation.h in Headers */ = {isa = PBXBuildFile; fileRef = AB615301192DA24600A2D8E9 /* UIView+WebCacheOperation.h */; };
  199 + AB615306192DA24600A2D8E9 /* UIView+WebCacheOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = AB615302192DA24600A2D8E9 /* UIView+WebCacheOperation.m */; };
  200 + AB615307192DA24600A2D8E9 /* UIView+WebCacheOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = AB615302192DA24600A2D8E9 /* UIView+WebCacheOperation.m */; };
  201 + AB615308192DA24600A2D8E9 /* UIView+WebCacheOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = AB615302192DA24600A2D8E9 /* UIView+WebCacheOperation.m */; };
196 ABBE71A718C43B4D00B75E91 /* UIImageView+HighlightedWebCache.h in Headers */ = {isa = PBXBuildFile; fileRef = ABBE71A518C43B4D00B75E91 /* UIImageView+HighlightedWebCache.h */; settings = {ATTRIBUTES = (Public, ); }; }; 202 ABBE71A718C43B4D00B75E91 /* UIImageView+HighlightedWebCache.h in Headers */ = {isa = PBXBuildFile; fileRef = ABBE71A518C43B4D00B75E91 /* UIImageView+HighlightedWebCache.h */; settings = {ATTRIBUTES = (Public, ); }; };
197 ABBE71A818C43B4D00B75E91 /* UIImageView+HighlightedWebCache.m in Sources */ = {isa = PBXBuildFile; fileRef = ABBE71A618C43B4D00B75E91 /* UIImageView+HighlightedWebCache.m */; }; 203 ABBE71A818C43B4D00B75E91 /* UIImageView+HighlightedWebCache.m in Sources */ = {isa = PBXBuildFile; fileRef = ABBE71A618C43B4D00B75E91 /* UIImageView+HighlightedWebCache.m */; };
198 ABBE71A918C43B5800B75E91 /* UIImageView+HighlightedWebCache.h in Headers */ = {isa = PBXBuildFile; fileRef = ABBE71A518C43B4D00B75E91 /* UIImageView+HighlightedWebCache.h */; settings = {ATTRIBUTES = (Public, ); }; }; 204 ABBE71A918C43B5800B75E91 /* UIImageView+HighlightedWebCache.h in Headers */ = {isa = PBXBuildFile; fileRef = ABBE71A518C43B4D00B75E91 /* UIImageView+HighlightedWebCache.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -290,6 +296,8 @@ @@ -290,6 +296,8 @@
290 5DA509F3187B68E7002FEB5C /* random.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = random.h; path = Vendors/libwebp/src/utils/random.h; sourceTree = "<group>"; }; 296 5DA509F3187B68E7002FEB5C /* random.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = random.h; path = Vendors/libwebp/src/utils/random.h; sourceTree = "<group>"; };
291 A18A6CC5172DC28500419892 /* UIImage+GIF.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIImage+GIF.h"; sourceTree = "<group>"; }; 297 A18A6CC5172DC28500419892 /* UIImage+GIF.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIImage+GIF.h"; sourceTree = "<group>"; };
292 A18A6CC6172DC28500419892 /* UIImage+GIF.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImage+GIF.m"; sourceTree = "<group>"; }; 298 A18A6CC6172DC28500419892 /* UIImage+GIF.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImage+GIF.m"; sourceTree = "<group>"; };
  299 + AB615301192DA24600A2D8E9 /* UIView+WebCacheOperation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIView+WebCacheOperation.h"; sourceTree = "<group>"; };
  300 + AB615302192DA24600A2D8E9 /* UIView+WebCacheOperation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIView+WebCacheOperation.m"; sourceTree = "<group>"; };
293 ABBE71A518C43B4D00B75E91 /* UIImageView+HighlightedWebCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIImageView+HighlightedWebCache.h"; sourceTree = "<group>"; }; 301 ABBE71A518C43B4D00B75E91 /* UIImageView+HighlightedWebCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIImageView+HighlightedWebCache.h"; sourceTree = "<group>"; };
294 ABBE71A618C43B4D00B75E91 /* UIImageView+HighlightedWebCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImageView+HighlightedWebCache.m"; sourceTree = "<group>"; }; 302 ABBE71A618C43B4D00B75E91 /* UIImageView+HighlightedWebCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImageView+HighlightedWebCache.m"; sourceTree = "<group>"; };
295 /* End PBXFileReference section */ 303 /* End PBXFileReference section */
@@ -392,6 +400,8 @@ @@ -392,6 +400,8 @@
392 53922D94148C56230056699D /* UIButton+WebCache.m */, 400 53922D94148C56230056699D /* UIButton+WebCache.m */,
393 53922D95148C56230056699D /* UIImageView+WebCache.h */, 401 53922D95148C56230056699D /* UIImageView+WebCache.h */,
394 53922D96148C56230056699D /* UIImageView+WebCache.m */, 402 53922D96148C56230056699D /* UIImageView+WebCache.m */,
  403 + AB615301192DA24600A2D8E9 /* UIView+WebCacheOperation.h */,
  404 + AB615302192DA24600A2D8E9 /* UIView+WebCacheOperation.m */,
395 ); 405 );
396 name = Categories; 406 name = Categories;
397 sourceTree = "<group>"; 407 sourceTree = "<group>";
@@ -532,6 +542,7 @@ @@ -532,6 +542,7 @@
532 53EDFCB317625CD800698166 /* quant_levels_dec.h in Headers */, 542 53EDFCB317625CD800698166 /* quant_levels_dec.h in Headers */,
533 53EDFCBA17625D1900698166 /* dsp.h in Headers */, 543 53EDFCBA17625D1900698166 /* dsp.h in Headers */,
534 53EDFCC617625DB200698166 /* bit_reader.h in Headers */, 544 53EDFCC617625DB200698166 /* bit_reader.h in Headers */,
  545 + AB615305192DA24600A2D8E9 /* UIView+WebCacheOperation.h in Headers */,
535 53EDFCCC17625DD700698166 /* color_cache.h in Headers */, 546 53EDFCCC17625DD700698166 /* color_cache.h in Headers */,
536 53EDFCD217625DFA00698166 /* lossless.h in Headers */, 547 53EDFCD217625DFA00698166 /* lossless.h in Headers */,
537 53EDFCE417625EB100698166 /* yuv.h in Headers */, 548 53EDFCE417625EB100698166 /* yuv.h in Headers */,
@@ -559,6 +570,7 @@ @@ -559,6 +570,7 @@
559 530E49E816464C25002868E7 /* SDWebImageOperation.h in Headers */, 570 530E49E816464C25002868E7 /* SDWebImageOperation.h in Headers */,
560 530E49EA16464C7C002868E7 /* SDWebImageDownloaderOperation.h in Headers */, 571 530E49EA16464C7C002868E7 /* SDWebImageDownloaderOperation.h in Headers */,
561 ABBE71A718C43B4D00B75E91 /* UIImageView+HighlightedWebCache.h in Headers */, 572 ABBE71A718C43B4D00B75E91 /* UIImageView+HighlightedWebCache.h in Headers */,
  573 + AB615303192DA24600A2D8E9 /* UIView+WebCacheOperation.h in Headers */,
562 A18A6CC7172DC28500419892 /* UIImage+GIF.h in Headers */, 574 A18A6CC7172DC28500419892 /* UIImage+GIF.h in Headers */,
563 53EDFB8A17623F7C00698166 /* UIImage+MultiFormat.h in Headers */, 575 53EDFB8A17623F7C00698166 /* UIImage+MultiFormat.h in Headers */,
564 ); 576 );
@@ -591,6 +603,7 @@ @@ -591,6 +603,7 @@
591 537D95B417ECC1FE0097C263 /* bit_reader.h in Headers */, 603 537D95B417ECC1FE0097C263 /* bit_reader.h in Headers */,
592 537D95B517ECC1FE0097C263 /* color_cache.h in Headers */, 604 537D95B517ECC1FE0097C263 /* color_cache.h in Headers */,
593 537D95B617ECC1FE0097C263 /* lossless.h in Headers */, 605 537D95B617ECC1FE0097C263 /* lossless.h in Headers */,
  606 + AB615304192DA24600A2D8E9 /* UIView+WebCacheOperation.h in Headers */,
594 537D95B717ECC1FE0097C263 /* alpha_processing.h in Headers */, 607 537D95B717ECC1FE0097C263 /* alpha_processing.h in Headers */,
595 5DA509F5187B68E7002FEB5C /* random.h in Headers */, 608 5DA509F5187B68E7002FEB5C /* random.h in Headers */,
596 537D95B817ECC1FE0097C263 /* yuv.h in Headers */, 609 537D95B817ECC1FE0097C263 /* yuv.h in Headers */,
@@ -749,6 +762,7 @@ @@ -749,6 +762,7 @@
749 A18A6CCA172DC28500419892 /* UIImage+GIF.m in Sources */, 762 A18A6CCA172DC28500419892 /* UIImage+GIF.m in Sources */,
750 53EDFB8D17623F7C00698166 /* UIImage+MultiFormat.m in Sources */, 763 53EDFB8D17623F7C00698166 /* UIImage+MultiFormat.m in Sources */,
751 53EDFB961762547D00698166 /* UIImage+WebP.m in Sources */, 764 53EDFB961762547D00698166 /* UIImage+WebP.m in Sources */,
  765 + AB615308192DA24600A2D8E9 /* UIView+WebCacheOperation.m in Sources */,
752 ABBE71AC18C43B6000B75E91 /* UIImageView+HighlightedWebCache.m in Sources */, 766 ABBE71AC18C43B6000B75E91 /* UIImageView+HighlightedWebCache.m in Sources */,
753 53EDFC9517625BE300698166 /* webp.c in Sources */, 767 53EDFC9517625BE300698166 /* webp.c in Sources */,
754 53EDFC9D17625C1100698166 /* vp8l.c in Sources */, 768 53EDFC9D17625C1100698166 /* vp8l.c in Sources */,
@@ -794,6 +808,7 @@ @@ -794,6 +808,7 @@
794 530E49EC16464C84002868E7 /* SDWebImageDownloaderOperation.m in Sources */, 808 530E49EC16464C84002868E7 /* SDWebImageDownloaderOperation.m in Sources */,
795 53406750167780C40042B59E /* SDWebImageCompat.m in Sources */, 809 53406750167780C40042B59E /* SDWebImageCompat.m in Sources */,
796 A18A6CC9172DC28500419892 /* UIImage+GIF.m in Sources */, 810 A18A6CC9172DC28500419892 /* UIImage+GIF.m in Sources */,
  811 + AB615306192DA24600A2D8E9 /* UIView+WebCacheOperation.m in Sources */,
797 5D5B9145188EE8DD006D06BD /* NSData+ImageContentType.m in Sources */, 812 5D5B9145188EE8DD006D06BD /* NSData+ImageContentType.m in Sources */,
798 53EDFB8C17623F7C00698166 /* UIImage+MultiFormat.m in Sources */, 813 53EDFB8C17623F7C00698166 /* UIImage+MultiFormat.m in Sources */,
799 ABBE71A818C43B4D00B75E91 /* UIImageView+HighlightedWebCache.m in Sources */, 814 ABBE71A818C43B4D00B75E91 /* UIImageView+HighlightedWebCache.m in Sources */,
@@ -805,6 +820,7 @@ @@ -805,6 +820,7 @@
805 buildActionMask = 2147483647; 820 buildActionMask = 2147483647;
806 files = ( 821 files = (
807 537D957517ECC1FE0097C263 /* SDImageCache.m in Sources */, 822 537D957517ECC1FE0097C263 /* SDImageCache.m in Sources */,
  823 + AB615307192DA24600A2D8E9 /* UIView+WebCacheOperation.m in Sources */,
808 537D957617ECC1FE0097C263 /* SDWebImageDecoder.m in Sources */, 824 537D957617ECC1FE0097C263 /* SDWebImageDecoder.m in Sources */,
809 537D957717ECC1FE0097C263 /* SDWebImageDownloader.m in Sources */, 825 537D957717ECC1FE0097C263 /* SDWebImageDownloader.m in Sources */,
810 537D957817ECC1FE0097C263 /* SDWebImageManager.m in Sources */, 826 537D957817ECC1FE0097C263 /* SDWebImageManager.m in Sources */,
@@ -8,9 +8,9 @@ @@ -8,9 +8,9 @@
8 8
9 #import "MKAnnotationView+WebCache.h" 9 #import "MKAnnotationView+WebCache.h"
10 #import "objc/runtime.h" 10 #import "objc/runtime.h"
  11 +#import "UIView+WebCacheOperation.h"
11 12
12 static char imageURLKey; 13 static char imageURLKey;
13 -static char operationKey;  
14 14
15 @implementation MKAnnotationView (WebCache) 15 @implementation MKAnnotationView (WebCache)
16 16
@@ -61,17 +61,12 @@ static char operationKey; @@ -61,17 +61,12 @@ static char operationKey;
61 } 61 }
62 }); 62 });
63 }]; 63 }];
64 - objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 64 + [self setImageLoadOperation:operation forKey:@"MKAnnotationViewImage"];
65 } 65 }
66 } 66 }
67 67
68 - (void)cancelCurrentImageLoad { 68 - (void)cancelCurrentImageLoad {
69 - // Cancel in progress downloader from queue  
70 - id <SDWebImageOperation> operation = objc_getAssociatedObject(self, &operationKey);  
71 - if (operation) {  
72 - [operation cancel];  
73 - objc_setAssociatedObject(self, &operationKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);  
74 - } 69 + [self cancelImageLoadOperationWithKey:@"MKAnnotationViewImage"];
75 } 70 }
76 71
77 @end 72 @end
@@ -184,8 +184,13 @@ @@ -184,8 +184,13 @@
184 - (void)setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletedBlock)completedBlock; 184 - (void)setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletedBlock)completedBlock;
185 185
186 /** 186 /**
187 - * Cancel the current download 187 + * Cancel the current image download
188 */ 188 */
189 -- (void)cancelCurrentImageLoad; 189 +- (void)cancelImageLoadForState:(UIControlState)state;
  190 +
  191 +/**
  192 + * Cancel the current backgroundImage download
  193 + */
  194 +- (void)cancelBackgroundImageLoadForState:(UIControlState)state;
190 195
191 @end 196 @end
@@ -8,9 +8,9 @@ @@ -8,9 +8,9 @@
8 8
9 #import "UIButton+WebCache.h" 9 #import "UIButton+WebCache.h"
10 #import "objc/runtime.h" 10 #import "objc/runtime.h"
  11 +#import "UIView+WebCacheOperation.h"
11 12
12 static char imageURLStorageKey; 13 static char imageURLStorageKey;
13 -static char operationKey;  
14 14
15 @implementation UIButton (WebCache) 15 @implementation UIButton (WebCache)
16 16
@@ -55,7 +55,7 @@ static char operationKey; @@ -55,7 +55,7 @@ static char operationKey;
55 - (void)setImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletedBlock)completedBlock { 55 - (void)setImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletedBlock)completedBlock {
56 56
57 [self setImage:placeholder forState:state]; 57 [self setImage:placeholder forState:state];
58 - [self cancelCurrentImageLoad]; 58 + [self cancelImageLoadForState:state];
59 59
60 if (!url) { 60 if (!url) {
61 [self.imageURLStorage removeObjectForKey:@(state)]; 61 [self.imageURLStorage removeObjectForKey:@(state)];
@@ -79,7 +79,7 @@ static char operationKey; @@ -79,7 +79,7 @@ static char operationKey;
79 } 79 }
80 }); 80 });
81 }]; 81 }];
82 - objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 82 + [self setImageLoadOperation:operation forState:state];
83 } 83 }
84 84
85 - (void)setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state { 85 - (void)setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state {
@@ -103,7 +103,7 @@ static char operationKey; @@ -103,7 +103,7 @@ static char operationKey;
103 } 103 }
104 104
105 - (void)setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletedBlock)completedBlock { 105 - (void)setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletedBlock)completedBlock {
106 - [self cancelCurrentImageLoad]; 106 + [self cancelImageLoadForState:state];
107 107
108 [self setBackgroundImage:placeholder forState:state]; 108 [self setBackgroundImage:placeholder forState:state];
109 109
@@ -122,18 +122,24 @@ static char operationKey; @@ -122,18 +122,24 @@ static char operationKey;
122 } 122 }
123 }); 123 });
124 }]; 124 }];
125 - objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 125 + [self setBackgroundImageLoadOperation:operation forState:state];
126 } 126 }
127 } 127 }
128 128
  129 +- (void)setImageLoadOperation:(id<SDWebImageOperation>)operation forState:(UIControlState)state {
  130 + [self setImageLoadOperation:operation forKey:[NSString stringWithFormat:@"UIButtonImageOperation%@", @(state)]];
  131 +}
129 132
130 -- (void)cancelCurrentImageLoad {  
131 - // Cancel in progress downloader from queue  
132 - id <SDWebImageOperation> operation = objc_getAssociatedObject(self, &operationKey);  
133 - if (operation) {  
134 - [operation cancel];  
135 - objc_setAssociatedObject(self, &operationKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);  
136 - } 133 +- (void)cancelImageLoadForState:(UIControlState)state {
  134 + [self cancelImageLoadOperationWithKey:[NSString stringWithFormat:@"UIButtonImageOperation%@", @(state)]];
  135 +}
  136 +
  137 +- (void)setBackgroundImageLoadOperation:(id<SDWebImageOperation>)operation forState:(UIControlState)state {
  138 + [self setImageLoadOperation:operation forKey:[NSString stringWithFormat:@"UIButtonBackgroundImageOperation%@", @(state)]];
  139 +}
  140 +
  141 +- (void)cancelBackgroundImageLoadForState:(UIControlState)state {
  142 + [self cancelImageLoadOperationWithKey:[NSString stringWithFormat:@"UIButtonBackgroundImageOperation%@", @(state)]];
137 } 143 }
138 144
139 - (NSMutableDictionary *)imageURLStorage; 145 - (NSMutableDictionary *)imageURLStorage;
@@ -7,7 +7,8 @@ @@ -7,7 +7,8 @@
7 */ 7 */
8 8
9 #import <UIKit/UIKit.h> 9 #import <UIKit/UIKit.h>
10 -#import "UIImageView+WebCache.h" 10 +#import "SDWebImageCompat.h"
  11 +#import "SDWebImageManager.h"
11 12
12 /** 13 /**
13 * Integrates SDWebImage async downloading and caching of remote images with UIImageView for highlighted state. 14 * Integrates SDWebImage async downloading and caching of remote images with UIImageView for highlighted state.
@@ -74,5 +75,9 @@ @@ -74,5 +75,9 @@
74 */ 75 */
75 - (void)setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletedBlock)completedBlock; 76 - (void)setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletedBlock)completedBlock;
76 77
  78 +/**
  79 + * Cancel the current download
  80 + */
  81 +- (void)cancelCurrentHighlightedImageLoad;
77 82
78 @end 83 @end
@@ -7,9 +7,9 @@ @@ -7,9 +7,9 @@
7 */ 7 */
8 8
9 #import "UIImageView+HighlightedWebCache.h" 9 #import "UIImageView+HighlightedWebCache.h"
10 -#import "objc/runtime.h" 10 +#import "UIView+WebCacheOperation.h"
11 11
12 -static char operationKey; 12 +#define UIImageViewHighlightedWebCacheOperationKey @"highlightedImage"
13 13
14 @implementation UIImageView (HighlightedWebCache) 14 @implementation UIImageView (HighlightedWebCache)
15 15
@@ -30,7 +30,7 @@ static char operationKey; @@ -30,7 +30,7 @@ static char operationKey;
30 } 30 }
31 31
32 - (void)setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletedBlock)completedBlock { 32 - (void)setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletedBlock)completedBlock {
33 - [self cancelCurrentImageLoad]; 33 + [self cancelCurrentHighlightedImageLoad];
34 34
35 if (url) { 35 if (url) {
36 __weak UIImageView *wself = self; 36 __weak UIImageView *wself = self;
@@ -52,8 +52,12 @@ static char operationKey; @@ -52,8 +52,12 @@ static char operationKey;
52 } 52 }
53 }); 53 });
54 }]; 54 }];
55 - objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 55 + [self setImageLoadOperation:operation forKey:UIImageViewHighlightedWebCacheOperationKey];
56 } 56 }
57 } 57 }
58 58
  59 +- (void)cancelCurrentHighlightedImageLoad {
  60 + [self cancelImageLoadOperationWithKey:UIImageViewHighlightedWebCacheOperationKey];
  61 +}
  62 +
59 @end 63 @end
@@ -153,6 +153,6 @@ @@ -153,6 +153,6 @@
153 */ 153 */
154 - (void)cancelCurrentImageLoad; 154 - (void)cancelCurrentImageLoad;
155 155
156 -- (void)cancelCurrentArrayLoad; 156 +- (void)cancelCurrentAnimationImagesLoad;
157 157
158 @end 158 @end
@@ -8,10 +8,9 @@ @@ -8,10 +8,9 @@
8 8
9 #import "UIImageView+WebCache.h" 9 #import "UIImageView+WebCache.h"
10 #import "objc/runtime.h" 10 #import "objc/runtime.h"
  11 +#import "UIView+WebCacheOperation.h"
11 12
12 static char imageURLKey; 13 static char imageURLKey;
13 -static char operationKey;  
14 -static char operationArrayKey;  
15 14
16 @implementation UIImageView (WebCache) 15 @implementation UIImageView (WebCache)
17 16
@@ -68,7 +67,7 @@ static char operationArrayKey; @@ -68,7 +67,7 @@ static char operationArrayKey;
68 } 67 }
69 }); 68 });
70 }]; 69 }];
71 - objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 70 + [self setImageLoadOperation:operation forKey:@"UIImageViewImageLoad"];
72 } 71 }
73 } 72 }
74 73
@@ -79,7 +78,7 @@ static char operationArrayKey; @@ -79,7 +78,7 @@ static char operationArrayKey;
79 78
80 - (void)setAnimationImagesWithURLs:(NSArray *)arrayOfURLs 79 - (void)setAnimationImagesWithURLs:(NSArray *)arrayOfURLs
81 { 80 {
82 - [self cancelCurrentArrayLoad]; 81 + [self cancelCurrentAnimationImagesLoad];
83 __weak UIImageView *wself = self; 82 __weak UIImageView *wself = self;
84 83
85 NSMutableArray *operationsArray = [[NSMutableArray alloc] init]; 84 NSMutableArray *operationsArray = [[NSMutableArray alloc] init];
@@ -106,27 +105,15 @@ static char operationArrayKey; @@ -106,27 +105,15 @@ static char operationArrayKey;
106 [operationsArray addObject:operation]; 105 [operationsArray addObject:operation];
107 } 106 }
108 107
109 - objc_setAssociatedObject(self, &operationArrayKey, [NSArray arrayWithArray:operationsArray], OBJC_ASSOCIATION_RETAIN_NONATOMIC); 108 + [self setImageLoadOperation:[NSArray arrayWithArray:operationsArray] forKey:@"UIImageViewAnimationImages"];
110 } 109 }
111 110
112 - (void)cancelCurrentImageLoad { 111 - (void)cancelCurrentImageLoad {
113 - // Cancel in progress downloader from queue  
114 - id <SDWebImageOperation> operation = objc_getAssociatedObject(self, &operationKey);  
115 - if (operation) {  
116 - [operation cancel];  
117 - objc_setAssociatedObject(self, &operationKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);  
118 - } 112 + [self cancelImageLoadOperationWithKey:@"UIImageViewImageLoad"];
119 } 113 }
120 114
121 -- (void)cancelCurrentArrayLoad {  
122 - // Cancel in progress downloader from queue  
123 - NSArray *operations = objc_getAssociatedObject(self, &operationArrayKey);  
124 - for (id <SDWebImageOperation> operation in operations) {  
125 - if (operation) {  
126 - [operation cancel];  
127 - }  
128 - }  
129 - objc_setAssociatedObject(self, &operationArrayKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 115 +- (void)cancelCurrentAnimationImagesLoad {
  116 + [self cancelImageLoadOperationWithKey:@"UIImageViewAnimationImages"];
130 } 117 }
131 118
132 @end 119 @end
  1 +//
  2 +// UIView+WebCacheOperation.h
  3 +// SDWebImage
  4 +//
  5 +// Created by Whirlwind on 14-5-22.
  6 +// Copyright (c) 2014年 Dailymotion. All rights reserved.
  7 +//
  8 +
  9 +#import <UIKit/UIKit.h>
  10 +#import "SDWebImageManager.h"
  11 +
  12 +@interface UIView (WebCacheOperation)
  13 +
  14 +- (void)setImageLoadOperation:(id)operation forKey:(NSString *)Key;
  15 +
  16 +- (void)cancelImageLoadOperationWithKey:(NSString *)key;
  17 +
  18 +- (void)removeImageLoadOperationWithKey:(NSString *)key;
  19 +
  20 +@end
  1 +//
  2 +// UIView+WebCacheOperation.m
  3 +// SDWebImage
  4 +//
  5 +// Created by Whirlwind on 14-5-22.
  6 +// Copyright (c) 2014年 Dailymotion. All rights reserved.
  7 +//
  8 +
  9 +#import "UIView+WebCacheOperation.h"
  10 +#import "objc/runtime.h"
  11 +
  12 +static char loadOperationKey;
  13 +
  14 +@implementation UIView (WebCacheOperation)
  15 +
  16 +- (NSMutableDictionary *)operationDictionary {
  17 + NSMutableDictionary *operations = objc_getAssociatedObject(self, &loadOperationKey);
  18 + if (operations) {
  19 + return operations;
  20 + }
  21 + operations = [NSMutableDictionary dictionary];
  22 + objc_setAssociatedObject(self, &loadOperationKey, operations, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  23 + return operations;
  24 +}
  25 +
  26 +- (void)setImageLoadOperation:(id)operation forKey:(NSString *)key {
  27 + [self cancelImageLoadOperationWithKey:key];
  28 + NSMutableDictionary *operationDictionary = [self operationDictionary];
  29 + [operationDictionary setObject:operation forKey:key];
  30 +}
  31 +
  32 +- (void)cancelImageLoadOperationWithKey:(NSString *)key {
  33 + // Cancel in progress downloader from queue
  34 + NSMutableDictionary *operationDictionary = [self operationDictionary];
  35 + id operations = [operationDictionary objectForKey:key];
  36 + if (operations) {
  37 + if ([operations isKindOfClass:[NSArray class]]) {
  38 + for (id <SDWebImageOperation> operation in operations) {
  39 + if (operation) {
  40 + [operation cancel];
  41 + }
  42 + }
  43 + } else {
  44 + [(id<SDWebImageOperation>) operations cancel];
  45 + }
  46 + [operationDictionary removeObjectForKey:key];
  47 + }
  48 +}
  49 +
  50 +- (void)removeImageLoadOperationWithKey:(NSString *)key {
  51 + NSMutableDictionary *operationDictionary = [self operationDictionary];
  52 + [operationDictionary removeObjectForKey:key];
  53 +}
  54 +
  55 +@end