I am trying to download multiple images using the same session and different download tasks as the Question says. I am able to download the first image but not the second one. In the didFinishDownloadingToURL I'am using if condition to identify the downloadTask and for a certain downloadTask set it to a certain imageView.
Here is my code and please be patient with me:
@interface ViewController ()
{
NSURLSessionConfiguration *sessionConfiguration;
NSURLSessionDownloadTask *firstDownloadTask;
NSURLSessionDownloadTask *secondDownloadTask;
NSURLSession *session;
UIImageView *firstImageHolder;
UIImageView *secondImageHolder;
}
@end
- (void)viewDidLoad
{
NSString *firstDownloadLink = @"http://letiarts.com/letiarts2014/wp-content/uploads/2014/04/icon_game.png";
sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];
firstImageHolder = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 45, 45)];
[_viewImages addSubview: firstImageHolder];
firstDownloadTask = [session downloadTaskWithURL:[NSURL URLWithString:firstDownloadLink]];
[firstDownloadTask resume];
//2
NSString *secondDownloadLink = @"http://downloads.bbc.co.uk/skillswise/images/promo/prefab-maths-game-336x189.jpg";
secondImageHolder = [[UIImageView alloc] initWithFrame:CGRectMake(50, 0, 45, 45)];
[_viewImages addSubview: secondImageHolder];
secondDownloadTask = [session downloadTaskWithURL:[NSURL URLWithString:secondDownloadLink]];
[secondDownloadTask resume];
}
And in the didFinishDownloadingToURL:
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
NSData *data = [NSData dataWithContentsOfURL:location];
if (downloadTask == firstDownloadTask) {
UIImage *theImage1 = [UIImage imageWithData:data];
[firstImageHolder setImage:theImage1];
NSLog(@"DOWNLOAD FIRST IMAGE FINISHED");
}
//download finished
if (downloadTask == secondDownloadTask) {
UIImage *theImage2 = [UIImage imageWithData:data];
[secondImageHolder setImage:theImage2];
NSLog(@"DOWNLOAD SECOND IMAGE FINISHED");
}
}
Thank you in advance!
I see that you already solved your own question, but I would like to add a few things.
@property NSURLSession * session;
Also you can identify the task through taskIdentifier
NSNumber * key = @(firstDownloadTask.taskIdentifier);
dispatch_async(dispatch_get_main_queue(), ^{
[firstImageHolder setImage:theImage1];
});
Sample Working Code
@interface ViewController: ...<...>
@property (nonatomic, strong) NSURLSession *session;
@end
@implementation ViewController
- (void)viewDidLoad
{
NSURLSessionConfiguration *sessionConfiguration;
//NSURLSessionDownloadTask *downloadTask; //can use same task if cancelling is not intended.
NSURLSessionDownloadTask *firstDownloadTask;
NSURLSessionDownloadTask *secondDownloadTask;
sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];
//1
NSString *firstDownloadLink = @"http://letiarts.com/letiarts2014/wp-content/uploads/2014/04/icon_game.png";
firstImageHolder = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 45, 45)];
[_viewImages addSubview: firstImageHolder];
/*downloadTask*/firstDownloadTask = [session downloadTaskWithURL:[NSURL URLWithString:firstDownloadLink]];
[/*downloadTask*/firstDownloadTask resume];
//2
NSString *secondDownloadLink = @"http://downloads.bbc.co.uk/skillswise/images/promo/prefab-maths-game-336x189.jpg";
secondImageHolder = [[UIImageView alloc] initWithFrame:CGRectMake(50, 0, 45, 45)];
[_viewImages addSubview: secondImageHolder];
/*downloadTask*/secondDownloadTask = [session downloadTaskWithURL:[NSURL URLWithString:secondDownloadLink]];
[/*downloadTask*/secondDownloadTask resume];
}
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
NSData *data = [NSData dataWithContentsOfURL:location];
if (downloadTask.taskIdentifier == 1) {
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *theImage1 = [UIImage imageWithData:data];
[firstImageHolder setImage:theImage1];
});
NSLog(@"DOWNLOAD FIRST IMAGE FINISHED");
}
//download finished
if (downloadTask.taskIdentifier == 2) {
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *theImage2 = [UIImage imageWithData:data];
[secondImageHolder setImage:theImage2];
});
NSLog(@"DOWNLOAD SECOND IMAGE FINISHED");
}
}
@end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With