AFNetworking 2.0 download multiple images with completion

后端 未结 3 378
再見小時候
再見小時候 2021-01-01 05:23

I\'m trying to figure out a way to download multiple images with AFNewtorking 2.0. I\'ve read a lot of posts here in SO, but can\'t find the answer I\'m looking for, hope yo

3条回答
  •  礼貌的吻别
    2021-01-01 06:14

    for(Photo *photo in array){
    
        //form the path where you want to save your downloaded image to
        NSString *constPath = [photo imageFullPath];
    
        //url of your photo
        NSURL *url = [NSURL URLWithString:photo.serverPath];
    
        AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:[NSURLRequest requestWithURL:url]];
        op.responseSerializer = [AFImageResponseSerializer serializer];
    
        op.outputStream = [NSOutputStream outputStreamToFileAtPath:constPath append:NO];
        op.queuePriority = NSOperationQueuePriorityLow;
        [op setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead){
    
        }];
    
        op.completionBlock = ^{
    
            //do whatever you want with the downloaded photo, it is stored in the path you create in constPath
        };
        [requestArray addObject:op];
    }
    
    NSArray *batches = [AFURLConnectionOperation batchOfRequestOperations:requestArray progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
    } completionBlock:^(NSArray *operations) {
    
        //after all operations are completed this block is called
        if (successBlock)
            successBlock();
    }];
    
    [[NSOperationQueue mainQueue] addOperations:batches waitUntilFinished:NO];
    

提交回复
热议问题