gpt4 book ai didi

ios - 如何使用同一 session 和不同的下载任务下载多个图像

转载 作者:可可西里 更新时间:2023-11-01 03:54:01 25 4
gpt4 key购买 nike

我正在尝试使用与问题所述相同的 session 和不同的下载任务来下载多个图像。我可以下载第一张图片,但不能下载第二张。在 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");

}
}

提前致谢!

最佳答案

我看到你已经解决了你自己的问题,但我想补充几点。

  1. 下面的实例不需要那么多属性就足够了。

@property NSURLSession * session;

也可以通过taskIdentifier来识别任务

NSNumber * key = @(firstDownloadTask.taskIdentifier);

  1. 在delegate中进行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

关于ios - 如何使用同一 session 和不同的下载任务下载多个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28114659/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com