如何使用相同的会话和不同的下载任务下载多个图像

时间:2015-01-23 16:46:24

标签: ios xcode nsurlsession

我正在尝试使用相同的会话和不同的下载任务下载多个图像,如问题所示。 我可以下载第一张图片而不是第二张图片。 在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");

    }
}

提前谢谢!

2 个答案:

答案 0 :(得分:4)

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

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

    @property NSURLSession * session;

    您还可以通过taskIdentifier

    识别任务
      

    NSNumber * key = @(firstDownloadTask.taskIdentifier);

    1. 在委托中执行ui操作时需要小心,如果没有从主线程调用它可能会导致冻结。为安全起见,应使用:
    2.   

      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
      

答案 1 :(得分:1)

代码很好,因为问题是在我的实际代码中我犯了一个错误,在第二个简历中我称之为第一个任务。