Remove unnecessary coupling between DMWebImageDownloader and DMImageCache

This commit is contained in:
Olivier Poitrey 2009-09-21 03:59:53 +02:00
parent 2177028001
commit 16431cb655
4 changed files with 24 additions and 21 deletions

View File

@ -8,7 +8,6 @@
#import <Foundation/Foundation.h>
@interface DMWebImageDownloader : NSOperation
{
NSURL *url;

View File

@ -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];
}

View File

@ -13,7 +13,7 @@
@interface DMWebImageView : UIImageView
{
UIImage *placeHolderImage;
DMWebImageDownloader *currentOperation;
DMWebImageDownloader *downloader;
}
- (void)setImageWithURL:(NSURL *)url;

View File

@ -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