From 3f2f360ee20702b70e88d18ac7c5a02ebd11d932 Mon Sep 17 00:00:00 2001 From: Olivier Poitrey Date: Sun, 3 Oct 2010 10:04:41 +0200 Subject: [PATCH] Add support for system network activity indicator show/hide via notification As showing/hiding the network activity indicator could conflict with your own code (i.e. the lib may hide the indicator when your code would still do some network stuff), the show/hide of the indicator isn't performed by the lib directly but SDWebImageDownloadStartNotification and SDWebImageDownloadStopNotification notifications are posted instead. This lets you handle the indicator visiblity by yourself. If you're lazy, you can use the SDNetworkActivityIndicator library (http://github.com/rs/SDNetworkActivityIndicator) to handle it automatically. Once added to your project, all you have to do is to import this lib in addition to the SDWebImage lib. Note that you should then use SDNetworkActivityIndicator for all your network status indicator visibility changes in your code if you don't want conflicts to happen. --- SDWebImageDownloader.h | 3 +++ SDWebImageDownloader.m | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/SDWebImageDownloader.h b/SDWebImageDownloader.h index 7e12add..aa2bafe 100644 --- a/SDWebImageDownloader.h +++ b/SDWebImageDownloader.h @@ -9,6 +9,9 @@ #import #import "SDWebImageDownloaderDelegate.h" +extern NSString *const SDWebImageDownloadStartNotification; +extern NSString *const SDWebImageDownloadStopNotification; + @interface SDWebImageDownloader : NSObject { @private diff --git a/SDWebImageDownloader.m b/SDWebImageDownloader.m index 1c8e918..2004df7 100644 --- a/SDWebImageDownloader.m +++ b/SDWebImageDownloader.m @@ -8,6 +8,9 @@ #import "SDWebImageDownloader.h" +NSString *const SDWebImageDownloadStartNotification = @"SDWebImageDownloadStartNotification"; +NSString *const SDWebImageDownloadStopNotification = @"SDWebImageDownloadStopNotification"; + @interface SDWebImageDownloader () @property (nonatomic, retain) NSURLConnection *connection; @end @@ -19,6 +22,19 @@ + (id)downloaderWithURL:(NSURL *)url delegate:(id)delegate { + // 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]; + } + SDWebImageDownloader *downloader = [[[SDWebImageDownloader alloc] init] autorelease]; downloader.url = url; downloader.delegate = delegate; @@ -44,6 +60,7 @@ if (connection) { self.imageData = [NSMutableData data]; + [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStartNotification object:nil]; } else { @@ -60,6 +77,7 @@ { [connection cancel]; self.connection = nil; + [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil]; } } @@ -70,10 +88,13 @@ [imageData appendData:data]; } +#pragma GCC diagnostic ignored "-Wundeclared-selector" - (void)connectionDidFinishLoading:(NSURLConnection *)aConnection { self.connection = nil; + [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil]; + if ([delegate respondsToSelector:@selector(imageDownloaderDidFinish:)]) { [delegate performSelector:@selector(imageDownloaderDidFinish:) withObject:self]; @@ -89,6 +110,8 @@ - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { + [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil]; + if ([delegate respondsToSelector:@selector(imageDownloader:didFailWithError:)]) { [delegate performSelector:@selector(imageDownloader:didFailWithError:) withObject:self withObject:error]; @@ -102,6 +125,7 @@ - (void)dealloc { + [[NSNotificationCenter defaultCenter] removeObserver:self]; [url release], url = nil; [connection release], connection = nil; [imageData release], imageData = nil;