0

I want to know is there way to download images with my code one by one ? Now async

    NSString *urlString = link;
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];

    AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
    requestOperation.responseSerializer = [AFImageResponseSerializer serializer];

    [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

        completion(operation, responseObject, nil);

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [RequestAPI sharedInstance].downloadsCount -= 1;
        DDLogError(@"FAIL download image: %@",error);
    }];

    [requestOperation start];

1 Answers1

0

Try this solution

  1. Create a NSObject class for downloading the images. Lets call this DownloadImages. The class has an array of images to be downloaded arrayOfImagesURL.
  2. Create a delegate for the class DownloadImages to make a call back when you finish downloading an image. View how to this in detail here.
  3. Implement AFNetworking as you like, you can also accomplish this by using the iOS 7 API NSURLSession. So in the completion block of your download task, you need to do 2 things:
    1. Continue downloading the next URL in arrayOfImagesURL.
    2. Do a call back with the delegate to the parent controller for processing.

Tell me if there's something need to be more clarify.

Community
  • 1
  • 1
Thanh-Nhon Nguyen
  • 3,402
  • 3
  • 28
  • 41