Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added custom activity indicator #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions AsyncImageView/AsyncImageView.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wobjc-missing-property-synthesis"

@protocol CustomAsyncImageViewActivityProtocol <NSObject>

-(void)updateProgress:(float)percent;
-(void)startAnimating;
-(void)stopAnimating;
@end

extern NSString *const AsyncImageLoadDidFinish;
extern NSString *const AsyncImageLoadDidFail;
Expand Down Expand Up @@ -76,11 +82,11 @@ extern NSString *const AsyncImageErrorKey;


@interface AsyncImageView : UIImageView

@property (nonatomic) UIView<CustomAsyncImageViewActivityProtocol>* customActivityView;
@property (nonatomic, assign) BOOL showActivityIndicator;
@property (nonatomic, assign) UIActivityIndicatorViewStyle activityIndicatorStyle;
@property (nonatomic, assign) NSTimeInterval crossfadeDuration;

@property (nonatomic, strong) NSURL *currentImageURL;
@end


Expand Down
55 changes: 43 additions & 12 deletions AsyncImageView/AsyncImageView.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -589,9 +596,9 @@ - (NSURL *)imageURL


@interface AsyncImageView ()
-(void)updateProgress:(NSNumber *)percent;

@property (nonatomic, strong) UIActivityIndicatorView *activityView;

@end


Expand Down Expand Up @@ -622,6 +629,13 @@ - (id)initWithCoder:(NSCoder *)aDecoder
return self;
}

-(void)setCustomActivityView:(UIView<CustomAsyncImageViewActivityProtocol>*)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];
Expand All @@ -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];
}
}

Expand All @@ -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
Expand Down