Remove unnecessary coupling between DMWebImageDownloader and DMImageCache
This commit is contained in:
parent
2177028001
commit
16431cb655
|
@ -8,7 +8,6 @@
|
|||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
|
||||
@interface DMWebImageDownloader : NSOperation
|
||||
{
|
||||
NSURL *url;
|
||||
|
|
|
@ -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];
|
||||
}
|
||||
|
||||
|
|
|
@ -13,7 +13,7 @@
|
|||
@interface DMWebImageView : UIImageView
|
||||
{
|
||||
UIImage *placeHolderImage;
|
||||
DMWebImageDownloader *currentOperation;
|
||||
DMWebImageDownloader *downloader;
|
||||
}
|
||||
|
||||
- (void)setImageWithURL:(NSURL *)url;
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue