如何使用AFNetworking下載圖片?一個「按順序」,我也意味着按順序執行success
塊。按照AFNetworking的要求下載圖片
最初我認爲使用NSOperationQueue
就足夠了,並將每個AFImageRequestOperation
設置爲下一個依賴項。就像這樣:
- (void) downloadImages
{
{ // Reset
[_downloadQueue cancelAllOperations];
_downloadQueue = [[NSOperationQueue alloc] init];
_images = [NSMutableArray array];
}
AFImageRequestOperation *previousOperation = nil;
for (NSInteger i = 0; i < _imageURLs.count; i++) {
NSURL *URL = [_imageURLs objectAtIndex:i];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
AFImageRequestOperation *operation = [AFImageRequestOperation
imageRequestOperationWithRequest:request
imageProcessingBlock:nil
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
[_images addObject:image];
NSLog(@"%d", i);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {}];
if (previousOperation) {
[operation addDependency:previousOperation];
}
previousOperation = operation;
[_downloadQueue addOperation:operation];
}
}
這是爲了打印i
的圖像下載時。但是,當請求已被緩存時,成功塊將按順序處理。我懷疑這是一個NSOperation
的限制,而不是AFNetworking。
我錯過了什麼嗎?