2014-05-15 85 views
4

我试图找出一种方法来下载与AFNewtorking 2.0多个图像。我在这里阅读了很多文章,但找不到我期待的答案,希望你们能帮助我。AFNetworking 2.0完成下载多个图像

问题是我想知道什么时候所有的下载都完成了,如果所有的图片都下载了。 所以我有一个图像URL的蚂蚁数组试图做这样的事情。

for(NSString *photoUrlString in self.photos){ 

     NSURL *url = [NSURL URLWithString:photoUrlString]; 
     AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:[NSURLRequest requestWithURL:url]]; 
     requestOperation.responseSerializer = [AFImageResponseSerializer serializer]; 
     [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 

     } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
      NSLog(@"Image error: %@", error); 
     }]; 
     [requestOperation start]; 
    } 

我找到了一些答案与把这些请求放入一个队列,并设置最大并发操作为1,但不知道如何工作真的。

任何帮助表示赞赏,在此先感谢!

+0

请问您可以发布您使用afnetworking 2.0实现的示例来下载多个图像。谢谢! –

+1

@ Yogesh.Lolusare.Apple检查我的答案,我正在流文件,因为我发现它更快。如果有人有更好的解决方案,请发布。 – Lukas

回答

3
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]; 
+0

谢谢。我会与我实施的。 –

+0

你需要一个AFHTTPRequestOperationManager进行批量图像加载,例如在tableview中? – JDG

2

试试这个:

// _group, _queue are iVar variable 
dispatch_group_t *_group = dispatch_group_create(); 
dispatch_queue_t *_queue = dispatch_queue_create("com.company.myqueue2", NULL); 

// all files download 
for(int i = 0 ; i < numberOfFileDownloads; i++){ 
    dispatch_group_async(_group, _queue, ^{ 
     // here is background thread; 
     // download file 
    }); 
} 


// all files are download successfully, this method is called 
dispatch_group_notify(_group, _queue, ^{ 
} 
1

退房+[AFURLConnectionOperation batchOfRequestOperations:progressBlock:completionBlock:]

虽然它没有记录,implementation是不言自明的。它也允许你监视进度。

在使用此方法之前,您需要有一个HTTP操作数组(如果您决定坚持使用基于NSURLConnection的AFNetworking实现)。