2009-09-21 09:38:30 +08:00
|
|
|
/*
|
|
|
|
* This file is part of the DMWebImage package.
|
|
|
|
* (c) Dailymotion - Olivier Poitrey <rs@dailymotion.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import "DMWebImageDownloader.h"
|
|
|
|
|
2009-09-21 09:59:53 +08:00
|
|
|
static NSOperationQueue *downloadQueue;
|
2009-09-21 09:38:30 +08:00
|
|
|
|
|
|
|
@implementation DMWebImageDownloader
|
|
|
|
|
|
|
|
@synthesize url, target, action;
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[url release];
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (id)downloaderWithURL:(NSURL *)url target:(id)target action:(SEL)action
|
|
|
|
{
|
|
|
|
DMWebImageDownloader *downloader = [[[DMWebImageDownloader alloc] init] autorelease];
|
|
|
|
downloader.url = url;
|
|
|
|
downloader.target = target;
|
|
|
|
downloader.action = action;
|
|
|
|
|
2009-09-21 09:59:53 +08:00
|
|
|
if (downloadQueue == nil)
|
2009-09-21 09:38:30 +08:00
|
|
|
{
|
2009-09-21 09:59:53 +08:00
|
|
|
downloadQueue = [[NSOperationQueue alloc] init];
|
|
|
|
downloadQueue.maxConcurrentOperationCount = 8;
|
2009-09-21 09:38:30 +08:00
|
|
|
}
|
|
|
|
|
2009-09-21 09:59:53 +08:00
|
|
|
[downloadQueue addOperation:downloader];
|
2009-09-21 09:38:30 +08:00
|
|
|
|
|
|
|
return downloader;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (void)setMaxConcurrentDownloads:(NSUInteger)max
|
|
|
|
{
|
2009-09-21 09:59:53 +08:00
|
|
|
if (downloadQueue == nil)
|
2009-09-21 09:38:30 +08:00
|
|
|
{
|
2009-09-21 09:59:53 +08:00
|
|
|
downloadQueue = [[NSOperationQueue alloc] init];
|
2009-09-21 09:38:30 +08:00
|
|
|
}
|
|
|
|
|
2009-09-21 09:59:53 +08:00
|
|
|
downloadQueue.maxConcurrentOperationCount = max;
|
2009-09-21 09:38:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)main
|
|
|
|
{
|
|
|
|
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
|
|
|
|
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
|
|
|
|
|
|
|
|
if (!self.isCancelled)
|
|
|
|
{
|
|
|
|
[target performSelector:action withObject:image];
|
|
|
|
}
|
|
|
|
|
|
|
|
[pool release];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|