I am trying to upload multiple images using the same session and different download tasks as the question. I can upload the first image, but not the second. In didFinishDownloadingToURL I use if the condition for identifying downloadTask and for a specific downloadTask set it to a specific 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];
And in the didFinishDownloadingToURL file:
- (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"); }
Thank you in advance!
source share