AFNetworkingを使用して、アプリのサーバーから json と画像をダウンロードしています。AFNetworking を使用して画像をシリアル順にダウンロードしたいのですが、画像がランダムな順序で提供されるため、応答が適切な順序ではありません。クリックするとカルーセルにサムネイルが表示され、選択した同じ画像の大きな画像を表示したいのですが、AFNetworking を使用すると、カルーセル画像をクリックするとランダムな画像が表示されます。
GCD でシリアル キューを使用してタスクを実行できますが、AFNetworking を使用してタスクを実行したいと考えています。それを行う方法を提案してください。
以下は、GCDを使用してシリアル順にダウンロードするための私のコードです
-(void)downloadImages:(NSMutableArray *)twitterThumbnailUrl withtwitterImages:(NSMutableArray *)twitterImageUrl
{
dispatch_queue_t queue;
queue = dispatch_queue_create("myImageQueue", NULL);
for(int i = 0; i<twitterThumbnailUrl.count; i++) {
NSURL *imageUrl = [NSURL URLWithString:[twitterThumbnailUrl objectAtIndex:i]];
NSURL *mainUrl = [NSURL URLWithString:[twitterImageUrl objectAtIndex:i]];
dispatch_async(queue, ^{
// do your stuff in the right order
NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];
NSData *imgData = [NSData dataWithContentsOfURL:mainUrl];
UIImage *image = [UIImage imageWithData:imageData];
UIImage *mainImage = [UIImage imageWithData:imgData];
if (image != NULL) {
[self.twitterImages addObject:image];
NSLog(@"num = %d", i);
}
if (mainImage != NULL) {
[self.celebImageArray addObject:mainImage];
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.carousel reloadData];
[spinner stopAnimating];
});
});
}
}
以下はAFNetworkingを使用したコードです
-(void)downloadTwitterImages:(NSString *)twitterImagesUrl
{
// download the photo
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:twitterImagesUrl]];
AFImageRequestOperation *operation = [AFImageRequestOperation
imageRequestOperationWithRequest:request
imageProcessingBlock:nil
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image)
{
if (image != nil) {
[self.celebImageArray addObject:image];
}
[self.carousel reloadData];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(@"%@", error.description);
}];
[operation start];
}
AFNetworkingで同じものをどのようにアーカイブしますか。助けてください
アップデート:
この投稿 [リンク] からこの方法を試したところ、画像が順番にダウンロードされています
これは私のコードです。ここで何か間違ったことをしている場合は修正してください
- (void) downloadTwitterImages:(NSMutableArray *)thumbnailArray {
{
[_thumbDownloadQueue cancelAllOperations];
_thumbDownloadQueue = [[NSOperationQueue alloc] init];
self.twitterImages = [[NSMutableArray alloc] init];
}
AFImageRequestOperation *previousOperation = nil;
for (NSUInteger i = 0; i<[thumbnailArray count]; i++) {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[thumbnailArray objectAtIndex:i]]];
AFImageRequestOperation *operation = [AFImageRequestOperation
imageRequestOperationWithRequest:request
imageProcessingBlock:nil
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image)
{
if (image != nil) {
NSLog(@"thumbnail downloaded %@", image);
[self.twitterImages addObject:image];
}
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(@"%@", error.description);
}];
if (previousOperation) {
[operation addDependency:previousOperation];
}
previousOperation = operation;
[_thumbDownloadQueue addOperation:operation];
}
}