From 074ce1f4cf4e5ad552fd62fcf958d60f15391515 Mon Sep 17 00:00:00 2001 From: Klaas Pieter Annema Date: Mon, 25 Nov 2013 16:37:26 -0500 Subject: [PATCH 1/6] Keep a reference to the image URL --- SDWebImage/UIImageView+WebCache.h | 2 ++ SDWebImage/UIImageView+WebCache.m | 11 ++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/SDWebImage/UIImageView+WebCache.h b/SDWebImage/UIImageView+WebCache.h index cfdd675..1bd6677 100644 --- a/SDWebImage/UIImageView+WebCache.h +++ b/SDWebImage/UIImageView+WebCache.h @@ -44,6 +44,8 @@ */ @interface UIImageView (WebCache) +- (NSURL *)imageURL; + /** * Set the imageView `image` with an `url`. * diff --git a/SDWebImage/UIImageView+WebCache.m b/SDWebImage/UIImageView+WebCache.m index e0de97d..201e955 100644 --- a/SDWebImage/UIImageView+WebCache.m +++ b/SDWebImage/UIImageView+WebCache.m @@ -9,6 +9,7 @@ #import "UIImageView+WebCache.h" #import "objc/runtime.h" +static char imageURLKey; static char operationKey; static char operationArrayKey; @@ -40,6 +41,8 @@ static char operationArrayKey; - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletedBlock)completedBlock { [self cancelCurrentImageLoad]; + objc_setAssociatedObject(self, &imageURLKey, url, OBJC_ASSOCIATION_RETAIN_NONATOMIC); + self.image = placeholder; if (!(options & SDWebImageDelayPlaceholder)) { self.image = placeholder; @@ -69,7 +72,13 @@ static char operationArrayKey; } } -- (void)setAnimationImagesWithURLs:(NSArray *)arrayOfURLs { +- (NSURL *)imageURL; +{ + return objc_getAssociatedObject(self, &imageURLKey); +} + +- (void)setAnimationImagesWithURLs:(NSArray *)arrayOfURLs +{ [self cancelCurrentArrayLoad]; __weak UIImageView *wself = self; From 77be243598990976a4b78f5e9549bd9c29bf61f2 Mon Sep 17 00:00:00 2001 From: Klaas Pieter Annema Date: Fri, 29 Nov 2013 11:39:01 -0500 Subject: [PATCH 2/6] Add documentation to imageURL property --- SDWebImage/UIImageView+WebCache.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/SDWebImage/UIImageView+WebCache.h b/SDWebImage/UIImageView+WebCache.h index 1bd6677..40e2f2c 100644 --- a/SDWebImage/UIImageView+WebCache.h +++ b/SDWebImage/UIImageView+WebCache.h @@ -44,6 +44,12 @@ */ @interface UIImageView (WebCache) +/** + * Get the current image URL. + * + * Note that because of the limitations of categories this property can get out of sync + * if you use setImage: directly. + */ - (NSURL *)imageURL; /** From 825207d1d099d08d0353c53d63adb8a280335c0e Mon Sep 17 00:00:00 2001 From: Klaas Pieter Annema Date: Fri, 29 Nov 2013 11:43:02 -0500 Subject: [PATCH 3/6] Add imageURL property to remaining categories --- SDWebImage/MKAnnotationView+WebCache.h | 8 ++++++++ SDWebImage/MKAnnotationView+WebCache.m | 10 +++++++++- SDWebImage/UIButton+WebCache.h | 8 ++++++++ SDWebImage/UIButton+WebCache.m | 10 +++++++++- 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/SDWebImage/MKAnnotationView+WebCache.h b/SDWebImage/MKAnnotationView+WebCache.h index fca8df0..2be81c5 100644 --- a/SDWebImage/MKAnnotationView+WebCache.h +++ b/SDWebImage/MKAnnotationView+WebCache.h @@ -14,6 +14,14 @@ */ @interface MKAnnotationView (WebCache) +/** + * Get the current image URL. + * + * Note that because of the limitations of categories this property can get out of sync + * if you use setImage: directly. + */ +- (NSURL *)imageURL; + /** * Set the imageView `image` with an `url`. * diff --git a/SDWebImage/MKAnnotationView+WebCache.m b/SDWebImage/MKAnnotationView+WebCache.m index c86a09d..c4c2f88 100644 --- a/SDWebImage/MKAnnotationView+WebCache.m +++ b/SDWebImage/MKAnnotationView+WebCache.m @@ -9,11 +9,18 @@ #import "MKAnnotationView+WebCache.h" #import "objc/runtime.h" +static char imageURLKey; static char operationKey; @implementation MKAnnotationView (WebCache) -- (void)setImageWithURL:(NSURL *)url { +- (NSURL *)imageURL; +{ + return objc_getAssociatedObject(self, &imageURLKey); +} + +- (void)setImageWithURL:(NSURL *)url +{ [self setImageWithURL:url placeholderImage:nil options:0 completed:nil]; } @@ -36,6 +43,7 @@ static char operationKey; - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletedBlock)completedBlock { [self cancelCurrentImageLoad]; + objc_setAssociatedObject(self, &imageURLKey, url, OBJC_ASSOCIATION_RETAIN_NONATOMIC); self.image = placeholder; if (url) { diff --git a/SDWebImage/UIButton+WebCache.h b/SDWebImage/UIButton+WebCache.h index 1f5d2c8..b663dfd 100644 --- a/SDWebImage/UIButton+WebCache.h +++ b/SDWebImage/UIButton+WebCache.h @@ -14,6 +14,14 @@ */ @interface UIButton (WebCache) +/** + * Get the current image URL. + * + * Note that because of the limitations of categories this property can get out of sync + * if you use setImage: directly. + */ +- (NSURL *)currentImageURL; + /** * Set the imageView `image` with an `url`. * diff --git a/SDWebImage/UIButton+WebCache.m b/SDWebImage/UIButton+WebCache.m index 7f849e7..785d7fc 100644 --- a/SDWebImage/UIButton+WebCache.m +++ b/SDWebImage/UIButton+WebCache.m @@ -9,11 +9,18 @@ #import "UIButton+WebCache.h" #import "objc/runtime.h" +static char imageURLKey; static char operationKey; @implementation UIButton (WebCache) -- (void)setImageWithURL:(NSURL *)url forState:(UIControlState)state { +- (NSURL *)currentImageURL; +{ + return objc_getAssociatedObject(self, &imageURLKey); +} + +- (void)setImageWithURL:(NSURL *)url forState:(UIControlState)state +{ [self setImageWithURL:url forState:state placeholderImage:nil options:0 completed:nil]; } @@ -36,6 +43,7 @@ static char operationKey; - (void)setImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletedBlock)completedBlock { [self cancelCurrentImageLoad]; + objc_setAssociatedObject(self, &imageURLKey, url, OBJC_ASSOCIATION_RETAIN_NONATOMIC); [self setImage:placeholder forState:state]; if (url) { From 67ed774c0cdbbfda6f64e70112b27e1bcd435a01 Mon Sep 17 00:00:00 2001 From: Klaas Pieter Annema Date: Fri, 29 Nov 2013 11:48:03 -0500 Subject: [PATCH 4/6] Scratch that, this won't work on UIButton --- SDWebImage/UIButton+WebCache.h | 8 -------- SDWebImage/UIButton+WebCache.m | 7 ------- 2 files changed, 15 deletions(-) diff --git a/SDWebImage/UIButton+WebCache.h b/SDWebImage/UIButton+WebCache.h index b663dfd..1f5d2c8 100644 --- a/SDWebImage/UIButton+WebCache.h +++ b/SDWebImage/UIButton+WebCache.h @@ -14,14 +14,6 @@ */ @interface UIButton (WebCache) -/** - * Get the current image URL. - * - * Note that because of the limitations of categories this property can get out of sync - * if you use setImage: directly. - */ -- (NSURL *)currentImageURL; - /** * Set the imageView `image` with an `url`. * diff --git a/SDWebImage/UIButton+WebCache.m b/SDWebImage/UIButton+WebCache.m index 785d7fc..250689e 100644 --- a/SDWebImage/UIButton+WebCache.m +++ b/SDWebImage/UIButton+WebCache.m @@ -9,16 +9,10 @@ #import "UIButton+WebCache.h" #import "objc/runtime.h" -static char imageURLKey; static char operationKey; @implementation UIButton (WebCache) -- (NSURL *)currentImageURL; -{ - return objc_getAssociatedObject(self, &imageURLKey); -} - - (void)setImageWithURL:(NSURL *)url forState:(UIControlState)state { [self setImageWithURL:url forState:state placeholderImage:nil options:0 completed:nil]; @@ -43,7 +37,6 @@ static char operationKey; - (void)setImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletedBlock)completedBlock { [self cancelCurrentImageLoad]; - objc_setAssociatedObject(self, &imageURLKey, url, OBJC_ASSOCIATION_RETAIN_NONATOMIC); [self setImage:placeholder forState:state]; if (url) { From b9389350cb6cc21e54b00ec73c38ae3c58b3c90c Mon Sep 17 00:00:00 2001 From: Klaas Pieter Annema Date: Fri, 29 Nov 2013 11:56:47 -0500 Subject: [PATCH 5/6] Store image URLs by state in the UIButton category --- SDWebImage/UIButton+WebCache.h | 12 ++++++++++++ SDWebImage/UIButton+WebCache.m | 30 ++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/SDWebImage/UIButton+WebCache.h b/SDWebImage/UIButton+WebCache.h index 1f5d2c8..1620571 100644 --- a/SDWebImage/UIButton+WebCache.h +++ b/SDWebImage/UIButton+WebCache.h @@ -14,6 +14,18 @@ */ @interface UIButton (WebCache) +/** + * Get the current image URL. + */ +- (NSURL *)currentImageURL; + +/** + * Get the image URL for a control state. + * + * @param state Which state you want to know the URL for. The values are described in UIControlState. + */ +- (NSURL *)imageURLForState:(UIControlState)state; + /** * Set the imageView `image` with an `url`. * diff --git a/SDWebImage/UIButton+WebCache.m b/SDWebImage/UIButton+WebCache.m index 250689e..2625fc8 100644 --- a/SDWebImage/UIButton+WebCache.m +++ b/SDWebImage/UIButton+WebCache.m @@ -9,10 +9,27 @@ #import "UIButton+WebCache.h" #import "objc/runtime.h" +static char imageURLStorageKey; static char operationKey; @implementation UIButton (WebCache) +- (NSURL *)currentImageURL; +{ + NSURL *url = self.imageURLStorage[@(self.state)]; + + if (!url) { + url = self.imageURLStorage[@(UIControlStateNormal)]; + } + + return url; +} + +- (NSURL *)imageURLForState:(UIControlState)state; +{ + return self.imageURLStorage[@(state)]; +} + - (void)setImageWithURL:(NSURL *)url forState:(UIControlState)state { [self setImageWithURL:url forState:state placeholderImage:nil options:0 completed:nil]; @@ -37,6 +54,8 @@ static char operationKey; - (void)setImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletedBlock)completedBlock { [self cancelCurrentImageLoad]; + self.imageURLStorage[@(state)] = url; + [self setImage:placeholder forState:state]; if (url) { @@ -112,4 +131,15 @@ static char operationKey; } } +- (NSMutableDictionary *)imageURLStorage; +{ + NSMutableDictionary *storage = objc_getAssociatedObject(self, &imageURLStorageKey); + if (!storage) { + storage = [NSMutableDictionary dictionary]; + objc_setAssociatedObject(self, &imageURLStorageKey, storage, OBJC_ASSOCIATION_RETAIN_NONATOMIC); + } + + return storage; +} + @end From 1ac6b92d9d68a560f80e3797e04edab1024caf08 Mon Sep 17 00:00:00 2001 From: Klaas Pieter Annema Date: Tue, 3 Dec 2013 16:41:00 -0500 Subject: [PATCH 6/6] Conform to coding style --- SDWebImage/UIButton+WebCache.m | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/SDWebImage/UIButton+WebCache.m b/SDWebImage/UIButton+WebCache.m index 2625fc8..afa3192 100644 --- a/SDWebImage/UIButton+WebCache.m +++ b/SDWebImage/UIButton+WebCache.m @@ -18,7 +18,8 @@ static char operationKey; { NSURL *url = self.imageURLStorage[@(self.state)]; - if (!url) { + if (!url) + { url = self.imageURLStorage[@(UIControlStateNormal)]; } @@ -134,7 +135,8 @@ static char operationKey; - (NSMutableDictionary *)imageURLStorage; { NSMutableDictionary *storage = objc_getAssociatedObject(self, &imageURLStorageKey); - if (!storage) { + if (!storage) + { storage = [NSMutableDictionary dictionary]; objc_setAssociatedObject(self, &imageURLStorageKey, storage, OBJC_ASSOCIATION_RETAIN_NONATOMIC); }