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
|
|
|
|
2010-10-03 16:04:41 +08:00
|
|
|
NSString *const SDWebImageDownloadStartNotification = @"SDWebImageDownloadStartNotification";
|
|
|
|
NSString *const SDWebImageDownloadStopNotification = @"SDWebImageDownloadStopNotification";
|
|
|
|
|
2010-06-09 10:09:18 +08:00
|
|
|
@interface SDWebImageDownloader ()
|
|
|
|
@property (nonatomic, retain) NSURLConnection *connection;
|
|
|
|
@end
|
2009-09-21 09:38:30 +08:00
|
|
|
|
2009-09-22 01:34:32 +08:00
|
|
|
@implementation SDWebImageDownloader
|
2011-05-06 16:30:50 +08:00
|
|
|
@synthesize url, delegate, connection, imageData, userInfo, lowPriority;
|
2009-09-21 09:38:30 +08:00
|
|
|
|
2010-06-09 10:09:18 +08:00
|
|
|
#pragma mark Public Methods
|
2009-09-21 09:38:30 +08:00
|
|
|
|
2009-09-24 05:22:48 +08:00
|
|
|
+ (id)downloaderWithURL:(NSURL *)url delegate:(id<SDWebImageDownloaderDelegate>)delegate
|
2009-09-21 09:38:30 +08:00
|
|
|
{
|
2011-07-14 03:23:13 +08:00
|
|
|
return [self downloaderWithURL:url delegate:delegate userInfo:nil];
|
2010-10-06 23:37:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (id)downloaderWithURL:(NSURL *)url delegate:(id<SDWebImageDownloaderDelegate>)delegate userInfo:(id)userInfo
|
|
|
|
{
|
|
|
|
|
2011-07-14 03:23:13 +08:00
|
|
|
return [self downloaderWithURL:url delegate:delegate userInfo:userInfo lowPriority:NO];
|
2011-05-06 16:30:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (id)downloaderWithURL:(NSURL *)url delegate:(id<SDWebImageDownloaderDelegate>)delegate userInfo:(id)userInfo lowPriority:(BOOL)lowPriority
|
|
|
|
{
|
2010-10-03 16:04:41 +08:00
|
|
|
// Bind SDNetworkActivityIndicator if available (download it here: http://github.com/rs/SDNetworkActivityIndicator )
|
|
|
|
// To use it, just add #import "SDNetworkActivityIndicator.h" in addition to the SDWebImage import
|
|
|
|
if (NSClassFromString(@"SDNetworkActivityIndicator"))
|
|
|
|
{
|
|
|
|
id activityIndicator = [NSClassFromString(@"SDNetworkActivityIndicator") performSelector:NSSelectorFromString(@"sharedActivityIndicator")];
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:activityIndicator
|
|
|
|
selector:NSSelectorFromString(@"startActivity")
|
|
|
|
name:SDWebImageDownloadStartNotification object:nil];
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:activityIndicator
|
|
|
|
selector:NSSelectorFromString(@"stopActivity")
|
|
|
|
name:SDWebImageDownloadStopNotification object:nil];
|
|
|
|
}
|
2011-07-14 03:23:13 +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;
|
2011-01-25 10:29:57 +08:00
|
|
|
downloader.userInfo = userInfo;
|
2011-05-06 16:30:50 +08:00
|
|
|
downloader.lowPriority = lowPriority;
|
2010-06-21 07:30:12 +08:00
|
|
|
[downloader performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:YES];
|
2009-09-21 09:38:30 +08:00
|
|
|
return downloader;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (void)setMaxConcurrentDownloads:(NSUInteger)max
|
|
|
|
{
|
2010-06-09 10:09:18 +08:00
|
|
|
// NOOP
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)start
|
|
|
|
{
|
|
|
|
// In order to prevent from potential duplicate caching (NSURLCache + SDImageCache) we disable the cache for image requests
|
|
|
|
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:15];
|
|
|
|
self.connection = [[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO] autorelease];
|
2011-05-06 16:30:50 +08:00
|
|
|
|
|
|
|
// If not in low priority mode, ensure we aren't blocked by UI manipulations (default runloop mode for NSURLConnection is NSEventTrackingRunLoopMode)
|
|
|
|
if (!lowPriority)
|
|
|
|
{
|
|
|
|
[connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
|
|
|
|
}
|
2010-06-09 10:09:18 +08:00
|
|
|
[connection start];
|
|
|
|
[request release];
|
|
|
|
|
|
|
|
if (connection)
|
|
|
|
{
|
|
|
|
self.imageData = [NSMutableData data];
|
2010-10-03 16:04:41 +08:00
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStartNotification object:nil];
|
2010-06-09 10:09:18 +08:00
|
|
|
}
|
|
|
|
else
|
2009-09-21 09:38:30 +08:00
|
|
|
{
|
2010-06-09 10:09:18 +08:00
|
|
|
if ([delegate respondsToSelector:@selector(imageDownloader:didFailWithError:)])
|
|
|
|
{
|
|
|
|
[delegate performSelector:@selector(imageDownloader:didFailWithError:) withObject:self withObject:nil];
|
|
|
|
}
|
2009-09-21 09:38:30 +08:00
|
|
|
}
|
2010-06-09 10:09:18 +08:00
|
|
|
}
|
2009-09-21 09:38:30 +08:00
|
|
|
|
2010-06-09 10:09:18 +08:00
|
|
|
- (void)cancel
|
|
|
|
{
|
|
|
|
if (connection)
|
|
|
|
{
|
|
|
|
[connection cancel];
|
|
|
|
self.connection = nil;
|
2010-10-03 16:04:41 +08:00
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil];
|
2010-06-09 10:09:18 +08:00
|
|
|
}
|
2009-09-21 09:38:30 +08:00
|
|
|
}
|
|
|
|
|
2010-06-09 10:09:18 +08:00
|
|
|
#pragma mark NSURLConnection (delegate)
|
|
|
|
|
|
|
|
- (void)connection:(NSURLConnection *)aConnection didReceiveData:(NSData *)data
|
2009-09-21 09:38:30 +08:00
|
|
|
{
|
2010-06-09 10:09:18 +08:00
|
|
|
[imageData appendData:data];
|
|
|
|
}
|
2009-09-21 09:38:30 +08:00
|
|
|
|
2010-10-03 16:04:41 +08:00
|
|
|
#pragma GCC diagnostic ignored "-Wundeclared-selector"
|
2010-06-09 10:09:18 +08:00
|
|
|
- (void)connectionDidFinishLoading:(NSURLConnection *)aConnection
|
|
|
|
{
|
|
|
|
self.connection = nil;
|
2010-06-02 07:37:39 +08:00
|
|
|
|
2010-10-03 16:04:41 +08:00
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil];
|
|
|
|
|
2010-08-29 08:56:47 +08:00
|
|
|
if ([delegate respondsToSelector:@selector(imageDownloaderDidFinish:)])
|
|
|
|
{
|
|
|
|
[delegate performSelector:@selector(imageDownloaderDidFinish:) withObject:self];
|
|
|
|
}
|
2011-07-14 03:23:13 +08:00
|
|
|
|
2010-06-09 10:09:18 +08:00
|
|
|
if ([delegate respondsToSelector:@selector(imageDownloader:didFinishWithImage:)])
|
2009-09-21 09:38:30 +08:00
|
|
|
{
|
2010-08-29 08:56:47 +08:00
|
|
|
UIImage *image = [[UIImage alloc] initWithData:imageData];
|
2009-09-24 05:22:48 +08:00
|
|
|
[delegate performSelector:@selector(imageDownloader:didFinishWithImage:) withObject:self withObject:image];
|
2010-08-29 08:56:47 +08:00
|
|
|
[image release];
|
2009-09-21 09:38:30 +08:00
|
|
|
}
|
2010-06-09 10:09:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
|
|
|
|
{
|
2010-10-03 16:04:41 +08:00
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil];
|
|
|
|
|
2010-06-09 10:09:18 +08:00
|
|
|
if ([delegate respondsToSelector:@selector(imageDownloader:didFailWithError:)])
|
|
|
|
{
|
|
|
|
[delegate performSelector:@selector(imageDownloader:didFailWithError:) withObject:self withObject:error];
|
|
|
|
}
|
|
|
|
|
|
|
|
self.connection = nil;
|
|
|
|
self.imageData = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark NSObject
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
2010-10-03 16:04:41 +08:00
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
2010-06-09 10:09:18 +08:00
|
|
|
[url release], url = nil;
|
|
|
|
[connection release], connection = nil;
|
|
|
|
[imageData release], imageData = nil;
|
2011-01-25 10:29:57 +08:00
|
|
|
[userInfo release], userInfo = nil;
|
2010-06-09 10:09:18 +08:00
|
|
|
[super dealloc];
|
2009-09-21 09:38:30 +08:00
|
|
|
}
|
|
|
|
|
2010-06-09 10:09:18 +08:00
|
|
|
|
2009-09-21 09:38:30 +08:00
|
|
|
@end
|