如何使用同一会话和不同的下载任务下载多个图像
How to download multiple images using same session and different download tasks
我正在尝试使用与问题所述相同的会话和不同的下载任务来下载多个图像。
我可以下载第一张图片,但不能下载第二张。
在 didFinishDownloadingToURL 中,我使用 if 条件来识别 downloadTask 并将某个 downloadTask 设置为某个 imageView。
这是我的代码,请耐心等待:
@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];
}
并且在 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");
}
}
提前致谢!
代码很好,问题是在我的实际代码中我犯了一个错误,在第二份简历中我调用了第一个任务。
我看到你已经解决了你自己的问题,但我想补充几点。
- 不需要那么多属性,下面的实例就够了。
@property NSURLSession * session;
也可以通过taskIdentifier来识别任务
NSNumber * key = @(firstDownloadTask.taskIdentifier);
- 在委托中进行ui操作时需要小心,如果不是从主线程调用,可能会导致卡顿。为了安全起见,应该使用:
dispatch_async(dispatch_get_main_queue(), ^{
[firstImageHolder setImage:theImage1];
});
示例工作代码
@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
我正在尝试使用与问题所述相同的会话和不同的下载任务来下载多个图像。 我可以下载第一张图片,但不能下载第二张。 在 didFinishDownloadingToURL 中,我使用 if 条件来识别 downloadTask 并将某个 downloadTask 设置为某个 imageView。
这是我的代码,请耐心等待:
@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];
}
并且在 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");
}
}
提前致谢!
代码很好,问题是在我的实际代码中我犯了一个错误,在第二份简历中我调用了第一个任务。
我看到你已经解决了你自己的问题,但我想补充几点。
- 不需要那么多属性,下面的实例就够了。
@property NSURLSession * session;
也可以通过taskIdentifier来识别任务
NSNumber * key = @(firstDownloadTask.taskIdentifier);
- 在委托中进行ui操作时需要小心,如果不是从主线程调用,可能会导致卡顿。为了安全起见,应该使用:
dispatch_async(dispatch_get_main_queue(), ^{
[firstImageHolder setImage:theImage1];
});
示例工作代码
@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