From 16431cb655746ccdaa255ab0c1d32db409fe61c0 Mon Sep 17 00:00:00 2001 From: Olivier Poitrey Date: Mon, 21 Sep 2009 03:59:53 +0200 Subject: [PATCH] Remove unnecessary coupling between DMWebImageDownloader and DMImageCache --- DMWebImageDownloader.h | 1 - DMWebImageDownloader.m | 19 ++++++++----------- DMWebImageView.h | 2 +- DMWebImageView.m | 23 +++++++++++++++-------- 4 files changed, 24 insertions(+), 21 deletions(-) diff --git a/DMWebImageDownloader.h b/DMWebImageDownloader.h index f8504cb..c6f8399 100644 --- a/DMWebImageDownloader.h +++ b/DMWebImageDownloader.h @@ -8,7 +8,6 @@ #import - @interface DMWebImageDownloader : NSOperation { NSURL *url; diff --git a/DMWebImageDownloader.m b/DMWebImageDownloader.m index 74a03de..bc9c750 100644 --- a/DMWebImageDownloader.m +++ b/DMWebImageDownloader.m @@ -7,9 +7,8 @@ */ #import "DMWebImageDownloader.h" -#import "DMImageCache.h" -static NSOperationQueue *queue; +static NSOperationQueue *downloadQueue; @implementation DMWebImageDownloader @@ -28,25 +27,25 @@ static NSOperationQueue *queue; downloader.target = target; downloader.action = action; - if (queue == nil) + if (downloadQueue == nil) { - queue = [[NSOperationQueue alloc] init]; - queue.maxConcurrentOperationCount = 8; + downloadQueue = [[NSOperationQueue alloc] init]; + downloadQueue.maxConcurrentOperationCount = 8; } - [queue addOperation:downloader]; + [downloadQueue addOperation:downloader]; return downloader; } + (void)setMaxConcurrentDownloads:(NSUInteger)max { - if (queue == nil) + if (downloadQueue == nil) { - queue = [[NSOperationQueue alloc] init]; + downloadQueue = [[NSOperationQueue alloc] init]; } - queue.maxConcurrentOperationCount = max; + downloadQueue.maxConcurrentOperationCount = max; } - (void)main @@ -60,8 +59,6 @@ static NSOperationQueue *queue; [target performSelector:action withObject:image]; } - [[DMImageCache sharedImageCache] storeImage:image forKey:[url absoluteString]]; - [pool release]; } diff --git a/DMWebImageView.h b/DMWebImageView.h index 4bccb49..6a04122 100644 --- a/DMWebImageView.h +++ b/DMWebImageView.h @@ -13,7 +13,7 @@ @interface DMWebImageView : UIImageView { UIImage *placeHolderImage; - DMWebImageDownloader *currentOperation; + DMWebImageDownloader *downloader; } - (void)setImageWithURL:(NSURL *)url; diff --git a/DMWebImageView.m b/DMWebImageView.m index e823eba..6375128 100644 --- a/DMWebImageView.m +++ b/DMWebImageView.m @@ -15,7 +15,7 @@ - (void)dealloc { [placeHolderImage release]; - [currentOperation release]; + [downloader release]; [super dealloc]; } @@ -23,11 +23,12 @@ - (void)setImageWithURL:(NSURL *)url { - if (currentOperation != nil) + if (downloader != nil) { - [currentOperation cancel]; // remove from queue - [currentOperation release]; - currentOperation = nil; + // Remove in progress downloader from queue + [downloader cancel]; + [downloader release]; + downloader = nil; } // Save the placeholder image in order to re-apply it when view is reused @@ -48,15 +49,21 @@ } else { - currentOperation = [[DMWebImageDownloader downloaderWithURL:url target:self action:@selector(downloadFinishedWithImage:)] retain]; + downloader = [[DMWebImageDownloader downloaderWithURL:url target:self action:@selector(downloadFinishedWithImage:)] retain]; } } - (void)downloadFinishedWithImage:(UIImage *)anImage { + // Apply image to the underlaying UIImageView self.image = anImage; - [currentOperation release]; - currentOperation = nil; + + // Store the image in the cache + [[DMImageCache sharedImageCache] storeImage:anImage forKey:[downloader.url absoluteString]]; + + // Free the downloader + [downloader release]; + downloader = nil; } @end