我正在使用AFNetworking在我的應用程序中從服務器下載json和圖像。我想使用AFNetworking以連續順序下載圖像,但響應的順序並不正確,因爲它以隨機順序顯示圖像。點擊旋轉木馬上的縮略圖我想要顯示相同的選定圖像的更大圖像,但通過使用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];
}
我怎麼會在archieve的AFNetworking相同的代碼。請幫助
UPDATE:
這個方法我試過從這個帖子[鏈接]和它的下載圖像,以便
這是我的代碼。請糾正我,如果我在這裏做任何錯誤
- (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];
}
}
我可以發送該URL的索引到的下載方法,但如何分配相同的索引作爲圖像響應隨機出現的圖像。你能幫我解決這個問題嗎? – suhit
我仍然無法解決您的方法的問題。你能否提供一些關於如何做到這一點的代碼。我剛剛添加更新到我的帖子,它的工作,但我仍然懷疑。謝謝 – suhit