2013-08-03 51 views
0

我正在使用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]; 

    } 

} 

回答

0

问题是,您的代码处理图像下载完成不使用图像URL的索引。所以,最终的顺序是由每个图像下载的速度决定的。

考虑类似于设置celebImageArray以充满NSNull的实例,并在接收到关联图像(使用循环中的索引,您将传递给下载方法)时替换每个项目。或者,使用字典以URL的形式存储图像,以便稍后找到正确的图像。


当你得到图像的URL列表,这样做:

self.celebImageArray = [NSMutableArray array]; 

for (id url in self.twitterThumbnailUrl) { 
    [self.celebImageArray addObject:[NSNull null]]; 
} 

现在,您有设置为默认值的项目,但所有的指标都是有效的阵列。

而不是

[self.celebImageArray addObject:image]; 

你将不得不

[self.celebImageArray replaceObjectAtIndex:index withObject:image]; 

它取代与真实项目的默认项。

当您尝试使用单元上的图像,你应该检查它是否是一个图像,而不是NSNull

if ([[self.celebImageArray objectAtIndex:index] isKindOfClass:[UIImage class]]) { 
    cell.imageView.image = [self.celebImageArray objectAtIndex:index]; 
} 
+0

我可以发送该URL的索引到的下载方法,但如何分配相同的索引作为图像响应随机出现的图像。你能帮我解决这个问题吗? – suhit

+0

我仍然无法解决您的方法的问题。你能否提供一些关于如何做到这一点的代码。我刚刚添加更新到我的帖子,它的工作,但我仍然怀疑。谢谢 – suhit

相关问题