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