diff --git a/SDWebImage/SDWebImageDownloader.m b/SDWebImage/SDWebImageDownloader.m index 81661c46..f08cff89 100644 --- a/SDWebImage/SDWebImageDownloader.m +++ b/SDWebImage/SDWebImageDownloader.m @@ -134,6 +134,13 @@ NSString *const kCompletedCallbackKey = @"completed"; if (callback) callback(image, error, finished); } }); + } + cancelled:^ + { + dispatch_async(dispatch_get_main_queue(), ^ + { + [self.URLCallbacks removeObjectForKey:url]; + }); }]; [self.downloadQueue addOperation:operation]; } diff --git a/SDWebImage/SDWebImageDownloaderOperation.h b/SDWebImage/SDWebImageDownloaderOperation.h index 35d7f107..154a4e5a 100644 --- a/SDWebImage/SDWebImageDownloaderOperation.h +++ b/SDWebImage/SDWebImageDownloaderOperation.h @@ -18,6 +18,7 @@ - (id)initWithRequest:(NSURLRequest *)request options:(SDWebImageDownloaderOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock - completed:(SDWebImageDownloaderCompletedBlock)completedBlock; + completed:(SDWebImageDownloaderCompletedBlock)completedBlock + cancelled:(void (^)())cancelBlock; @end diff --git a/SDWebImage/SDWebImageDownloaderOperation.m b/SDWebImage/SDWebImageDownloaderOperation.m index 8ca83081..c8a91cf8 100644 --- a/SDWebImage/SDWebImageDownloaderOperation.m +++ b/SDWebImage/SDWebImageDownloaderOperation.m @@ -14,6 +14,7 @@ @property (strong, nonatomic) SDWebImageDownloaderProgressBlock progressBlock; @property (strong, nonatomic) SDWebImageDownloaderCompletedBlock completedBlock; +@property (strong, nonatomic) void (^cancelBlock)(); @property (assign, nonatomic, getter = isExecuting) BOOL executing; @property (assign, nonatomic, getter = isFinished) BOOL finished; @@ -28,7 +29,7 @@ size_t width, height; } -- (id)initWithRequest:(NSURLRequest *)request options:(SDWebImageDownloaderOptions)options progress:(void (^)(NSUInteger, long long))progressBlock completed:(void (^)(UIImage *, NSError *, BOOL))completedBlock +- (id)initWithRequest:(NSURLRequest *)request options:(SDWebImageDownloaderOptions)options progress:(void (^)(NSUInteger, long long))progressBlock completed:(void (^)(UIImage *, NSError *, BOOL))completedBlock cancelled:(void (^)())cancelBlock { if ((self = [super init])) { @@ -36,6 +37,7 @@ _options = options; _progressBlock = progressBlock; _completedBlock = completedBlock; + _cancelBlock = cancelBlock; _executing = NO; _finished = NO; _expectedSize = 0; @@ -95,14 +97,20 @@ if (self.isExecuting) self.executing = NO; } + if (self.cancelBlock) self.cancelBlock(); + self.cancelBlock = nil; + self.completedBlock = nil; + self.progressBlock = nil; self.connection = nil; self.imageData = nil; + } - (void)done { self.finished = YES; self.executing = NO; + self.cancelBlock = nil; self.completedBlock = nil; self.progressBlock = nil; self.connection = nil;