diff --git a/AsyncImageView/AsyncImageView.h b/AsyncImageView/AsyncImageView.h index 7b1b64d..ad89a8e 100755 --- a/AsyncImageView/AsyncImageView.h +++ b/AsyncImageView/AsyncImageView.h @@ -35,6 +35,12 @@ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wobjc-missing-property-synthesis" +@protocol CustomAsyncImageViewActivityProtocol + +-(void)updateProgress:(float)percent; +-(void)startAnimating; +-(void)stopAnimating; +@end extern NSString *const AsyncImageLoadDidFinish; extern NSString *const AsyncImageLoadDidFail; @@ -76,11 +82,11 @@ extern NSString *const AsyncImageErrorKey; @interface AsyncImageView : UIImageView - +@property (nonatomic) UIView* customActivityView; @property (nonatomic, assign) BOOL showActivityIndicator; @property (nonatomic, assign) UIActivityIndicatorViewStyle activityIndicatorStyle; @property (nonatomic, assign) NSTimeInterval crossfadeDuration; - +@property (nonatomic, strong) NSURL *currentImageURL; @end diff --git a/AsyncImageView/AsyncImageView.m b/AsyncImageView/AsyncImageView.m index a366de9..f88321a 100755 --- a/AsyncImageView/AsyncImageView.m +++ b/AsyncImageView/AsyncImageView.m @@ -62,6 +62,7 @@ @interface AsyncImageConnection : NSObject @property (nonatomic, assign) SEL failure; @property (nonatomic, getter = isLoading) BOOL loading; @property (nonatomic, getter = isCancelled) BOOL cancelled; +@property (nonatomic) float expectedLength; - (AsyncImageConnection *)initWithURL:(NSURL *)URL cache:(NSCache *)cache @@ -205,15 +206,21 @@ - (void)processDataInBackground:(NSData *)data } } -- (void)connection:(__unused NSURLConnection *)connection didReceiveResponse:(__unused NSURLResponse *)response +- (void)connection:(__unused NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { self.data = [NSMutableData data]; + self.expectedLength = response.expectedContentLength; } - (void)connection:(__unused NSURLConnection *)connection didReceiveData:(NSData *)data { //add data [self.data appendData:data]; + + double percent = (double)self.data.length / self.expectedLength; + SEL selector = NSSelectorFromString(@"updateProgress:"); + if ([self.target respondsToSelector:selector]) + [self.target performSelector:selector withObject:@(percent) afterDelay:0]; } - (void)connectionDidFinishLoading:(__unused NSURLConnection *)connection @@ -577,7 +584,7 @@ @implementation UIImageView(AsyncImageView) - (void)setImageURL:(NSURL *)imageURL { - [[AsyncImageLoader sharedLoader] loadImageWithURL:imageURL target:self action:@selector(setImage:)]; + [[AsyncImageLoader sharedLoader] loadImageWithURL:imageURL target:self action:@selector(setImage:)]; } - (NSURL *)imageURL @@ -589,9 +596,9 @@ - (NSURL *)imageURL @interface AsyncImageView () +-(void)updateProgress:(NSNumber *)percent; @property (nonatomic, strong) UIActivityIndicatorView *activityView; - @end @@ -622,6 +629,13 @@ - (id)initWithCoder:(NSCoder *)aDecoder return self; } +-(void)setCustomActivityView:(UIView*)customActivityView { + _customActivityView = customActivityView; + _customActivityView.center = CGPointMake(self.bounds.size.width / 2.0f, self.bounds.size.height / 2.0f); + + [self addSubview:_customActivityView]; +} + - (void)setImageURL:(NSURL *)imageURL { UIImage *image = [[AsyncImageLoader sharedLoader].cache objectForKey:imageURL]; @@ -631,17 +645,23 @@ - (void)setImageURL:(NSURL *)imageURL return; } super.imageURL = imageURL; + self.currentImageURL = imageURL; if (self.showActivityIndicator && !self.image && imageURL) { - if (self.activityView == nil) - { - self.activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:self.activityIndicatorStyle]; - self.activityView.hidesWhenStopped = YES; - self.activityView.center = CGPointMake(self.bounds.size.width / 2.0f, self.bounds.size.height / 2.0f); - self.activityView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin; - [self addSubview:self.activityView]; + if (self.customActivityView) { + [self.customActivityView startAnimating]; + } + else { + if (self.activityView == nil) + { + self.activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:self.activityIndicatorStyle]; + self.activityView.hidesWhenStopped = YES; + self.activityView.center = CGPointMake(self.bounds.size.width / 2.0f, self.bounds.size.height / 2.0f); + self.activityView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin; + [self addSubview:self.activityView]; + } + [self.activityView startAnimating]; } - [self.activityView startAnimating]; } } @@ -663,7 +683,18 @@ - (void)setImage:(UIImage *)image [self.layer addAnimation:animation forKey:nil]; } super.image = image; - [self.activityView stopAnimating]; + if (!self.customActivityView) { + [self.activityView stopAnimating]; + } + else { + [self.customActivityView stopAnimating]; + } +} + +-(void)updateProgress:(NSNumber *)percent { + if (self.customActivityView) { + [self.customActivityView updateProgress:[percent floatValue]]; + } } - (void)dealloc