From 251a1f9a87f6503698a77012fa8de085b2658737 Mon Sep 17 00:00:00 2001 From: Diego Chohfi Date: Mon, 16 Jun 2014 11:46:19 -0300 Subject: [PATCH] #761 fixing potential bug when sending a nil url for UIButton+WebCache --- SDWebImage/UIButton+WebCache.m | 44 ++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/SDWebImage/UIButton+WebCache.m b/SDWebImage/UIButton+WebCache.m index afa319259..909c8721e 100644 --- a/SDWebImage/UIButton+WebCache.m +++ b/SDWebImage/UIButton+WebCache.m @@ -53,29 +53,33 @@ - (void)setImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderI } - (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) { - __weak UIButton *wself = self; - id operation = [SDWebImageManager.sharedManager downloadWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) { - if (!wself) return; - dispatch_main_sync_safe(^{ - __strong UIButton *sself = wself; - if (!sself) return; - if (image) { - [sself setImage:image forState:state]; - } - if (completedBlock && finished) { - completedBlock(image, error, cacheType); - } - }); - }]; - objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC); + [self cancelCurrentImageLoad]; + + if (!url) { + [self.imageURLStorage removeObjectForKey:@(state)]; + + return; } + + self.imageURLStorage[@(state)] = url; + + __weak UIButton *wself = self; + id operation = [SDWebImageManager.sharedManager downloadWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) { + if (!wself) return; + dispatch_main_sync_safe(^{ + __strong UIButton *sself = wself; + if (!sself) return; + if (image) { + [sself setImage:image forState:state]; + } + if (completedBlock && finished) { + completedBlock(image, error, cacheType); + } + }); + }]; + objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } - (void)setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state {