2009-09-21 09:38:30 +08:00
|
|
|
/*
|
2009-09-22 01:34:32 +08:00
|
|
|
* This file is part of the SDWebImage package.
|
|
|
|
* (c) Olivier Poitrey <rs@dailymotion.com>
|
2009-09-21 09:38:30 +08:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2009-09-22 01:34:32 +08:00
|
|
|
#import "SDWebImageDownloader.h"
|
2009-09-21 09:38:30 +08:00
|
|
|
|
2009-09-21 09:59:53 +08:00
|
|
|
static NSOperationQueue *downloadQueue;
|
2009-09-21 09:38:30 +08:00
|
|
|
|
2009-09-22 01:34:32 +08:00
|
|
|
@implementation SDWebImageDownloader
|
2009-09-21 09:38:30 +08:00
|
|
|
|
2009-09-24 05:22:48 +08:00
|
|
|
@synthesize url, delegate;
|
2009-09-21 09:38:30 +08:00
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[url release];
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2009-09-24 05:22:48 +08:00
|
|
|
+ (id)downloaderWithURL:(NSURL *)url delegate:(id<SDWebImageDownloaderDelegate>)delegate
|
2009-09-21 09:38:30 +08:00
|
|
|
{
|
2009-09-22 01:34:32 +08:00
|
|
|
SDWebImageDownloader *downloader = [[[SDWebImageDownloader alloc] init] autorelease];
|
2009-09-21 09:38:30 +08:00
|
|
|
downloader.url = url;
|
2009-09-24 05:22:48 +08:00
|
|
|
downloader.delegate = delegate;
|
2009-09-21 09:38:30 +08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-03-17 19:42:29 +08:00
|
|
|
- (void)main
|
2009-09-21 09:38:30 +08:00
|
|
|
{
|
|
|
|
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
|
2010-03-17 19:42:29 +08:00
|
|
|
// In order to prevent from potential duplicate caching (NSURLCache + SDImageCache) we disable the cache for image requests
|
|
|
|
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:5];
|
|
|
|
UIImage *image = [UIImage imageWithData:[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:NULL]];
|
2009-09-21 09:38:30 +08:00
|
|
|
|
2009-09-24 05:22:48 +08:00
|
|
|
if (!self.isCancelled && [delegate respondsToSelector:@selector(imageDownloader:didFinishWithImage:)])
|
2009-09-21 09:38:30 +08:00
|
|
|
{
|
2009-09-24 05:22:48 +08:00
|
|
|
[delegate performSelector:@selector(imageDownloader:didFinishWithImage:) withObject:self withObject:image];
|
2009-09-21 09:38:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[pool release];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|