Created
November 15, 2012 21:15
-
-
Save scottandrus/4081316 to your computer and use it in GitHub Desktop.
AFNetworking Image Downloads with Download Progress Block
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)downloadMultiAFN { | |
// Basic Activity Indicator to indicate download | |
UIActivityIndicatorView *loading = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; | |
[loading startAnimating]; | |
[self.imageView.superview addSubview:loading]; | |
loading.center = self.imageView.center; | |
// Create a request from the url, make an AFImageRequestOperation initialized with that request | |
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.picUrl]]; | |
AFImageRequestOperation *op = [[AFImageRequestOperation alloc] initWithRequest:request]; | |
// Set a download progress block for the operation | |
[op setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) { | |
if ([op.request.URL.absoluteString isEqualToString:@"http://www.pleiade.org/images/hubble-m45_large.jpg"]) { | |
self.progressBar.progress = (float) totalBytesRead/totalBytesExpectedToRead; | |
} else self.progressBar2.progress = (float) totalBytesRead/totalBytesExpectedToRead; | |
}]; | |
// Set a completion block for the operation | |
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { | |
self.imageView.image = responseObject; | |
self.image = responseObject; | |
if ([op.request.URL.absoluteString isEqualToString:@"http://www.pleiade.org/images/hubble-m45_large.jpg"]) { | |
self.progressBar.progress = 0; | |
} else self.progressBar2.progress = 0; | |
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {}]; | |
// Start the image download operation | |
[op start]; | |
// Remove the activity indicator | |
[loading stopAnimating]; | |
[loading removeFromSuperview]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment