Skip to content

Commit

Permalink
SDWebImage#761 fixing potential bug when sending a nil url for UIButt…
Browse files Browse the repository at this point in the history
…on+WebCache
  • Loading branch information
dchohfi authored and devedup committed Sep 10, 2014
1 parent 09ed6bc commit 251a1f9
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions SDWebImage/UIButton+WebCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 <SDWebImageOperation> 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 <SDWebImageOperation> 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 {
Expand Down

0 comments on commit 251a1f9

Please sign in to comment.