diff --git a/SDWebImage/MKAnnotationView+WebCache.h b/SDWebImage/MKAnnotationView+WebCache.h index fca8df09..2be81c5b 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 c86a09dc..c4c2f88f 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 1f5d2c82..16205718 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 7f849e77..afa31925 100644 --- a/SDWebImage/UIButton+WebCache.m +++ b/SDWebImage/UIButton+WebCache.m @@ -9,11 +9,30 @@ #import "UIButton+WebCache.h" #import "objc/runtime.h" +static char imageURLStorageKey; static char operationKey; @implementation UIButton (WebCache) -- (void)setImageWithURL:(NSURL *)url forState:(UIControlState)state { +- (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]; } @@ -36,6 +55,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) { @@ -111,4 +132,16 @@ 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 diff --git a/SDWebImage/UIImageView+WebCache.h b/SDWebImage/UIImageView+WebCache.h index cfdd6757..40e2f2cc 100644 --- a/SDWebImage/UIImageView+WebCache.h +++ b/SDWebImage/UIImageView+WebCache.h @@ -44,6 +44,14 @@ */ @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; + /** * Set the imageView `image` with an `url`. * diff --git a/SDWebImage/UIImageView+WebCache.m b/SDWebImage/UIImageView+WebCache.m index e0de97d0..201e9554 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;