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-06-09 10:09:18 +08:00
|
|
|
@interface SDWebImageDownloader ()
|
|
|
|
@property (nonatomic, retain) NSURLConnection *connection;
|
|
|
|
@property (nonatomic, retain) NSMutableData *imageData;
|
|
|
|
@end
|
2009-09-21 09:38:30 +08:00
|
|
|
|
2009-09-22 01:34:32 +08:00
|
|
|
@implementation SDWebImageDownloader
|
2010-06-09 10:09:18 +08:00
|
|
|
@synthesize url, delegate, connection, imageData;
|
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
|
|
|
{
|
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;
|
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];
|
|
|
|
// Ensure we aren't blocked by UI manipulations (default runloop mode for NSURLConnection is NSEventTrackingRunLoopMode)
|
|
|
|
[connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
|
|
|
|
[connection start];
|
|
|
|
[request release];
|
|
|
|
|
|
|
|
if (connection)
|
|
|
|
{
|
|
|
|
self.imageData = [NSMutableData data];
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
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-06-09 10:09:18 +08:00
|
|
|
- (void)connectionDidFinishLoading:(NSURLConnection *)aConnection
|
|
|
|
{
|
|
|
|
UIImage *image = [[UIImage alloc] initWithData:imageData];
|
|
|
|
self.imageData = nil;
|
|
|
|
self.connection = nil;
|
2010-06-02 07:37:39 +08:00
|
|
|
|
2010-06-09 10:09:18 +08:00
|
|
|
if ([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
|
|
|
}
|
|
|
|
|
2010-06-09 10:09:18 +08:00
|
|
|
[image release];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
|
|
|
|
{
|
|
|
|
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
|
|
|
|
{
|
|
|
|
[url release], url = nil;
|
|
|
|
[connection release], connection = nil;
|
|
|
|
[imageData release], imageData = nil;
|
|
|
|
[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
|