我设置了一个循环,用于下载一系列图像,稍后我将使用这些图像使用 UIImageView
的 animationImages
属性制作动画。我想知道我的循环中的所有 block 何时完成执行以便我可以开始动画,并且想知道我如何才能知道它们何时完成?谢谢!
for (PFObject *pictureObject in objects){
PFFile *imageFile = [pictureObject objectForKey:@"image"];
NSURL *imageFileURL = [[NSURL alloc] initWithString:imageFile.url];
NSURLRequest *imageRequest = [NSURLRequest requestWithURL:imageFileURL];
[tokenImageView setImageWithURLRequest:imageRequest placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
[self.downloadedUIImages addObject:image]; //This is a mutableArray that will later be set to an UIImageView's animnationImages
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(@"Error %@", error);
}];
}
//When I know all the blocks have finished downloading, I will then to animate the downloaded images.
编辑:错误 -999
有问题
我在执行提供的答案中的代码时遇到以下问题:Domain=NSURLErrorDomain Code=-999 "The operation couldn't be completed. (NSURLErrorDomain error -999.)"
快速搜索 reveals Error -999
的意思是“在上一个请求完成之前发出了另一个请求”......这里肯定是这种情况,因为我正在快速连续发出多个请求。建议修复建议 here对我不起作用,因为它只会成功下载一个 UIImage(最后一个请求),而之前的失败。我想知道这里或 AFNetworking 中是否有我应该考虑的解决方法?谢谢!
编辑 2:基于@David 解决方案的工作代码
for (PFObject *pictureObject in objects){
PFFile *imageFile = [pictureObject objectForKey:@"image"];
NSURL *imageFileURL = [[NSURL alloc] initWithString:imageFile.url];
NSURLRequest *imageRequest = [NSURLRequest requestWithURL:imageFileURL];
AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:imageRequest];
requestOperation.responseSerializer = [AFImageResponseSerializer serializer];
dispatch_group_enter(group);
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Response: %@", responseObject);
UIImage *retrivedImage = (UIImage *)responseObject;
[self.downloadedUIImages addObject:retrivedImage];
dispatch_group_leave(group);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Image error: %@", error);
dispatch_group_leave(group);
}];
[requestOperation start];
counter ++;
}
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"Horray everything has completed");
NSLog(@"What is here %@", self.downloadedUIImages);
NSLog(@"Done");
});
最佳答案
创建一个调度组,在for循环中进入组,在完成 block 中离开组。然后你可以使用 dispatch_group_notify 找出所有 block 何时完成:
dispatch_group_t group = dispatch_group_create();
for (PFObject *pictureObject in objects){
PFFile *imageFile = [pictureObject objectForKey:@"image"];
NSURL *imageFileURL = [[NSURL alloc] initWithString:imageFile.url];
NSURLRequest *imageRequest = [NSURLRequest requestWithURL:imageFileURL];
dispatch_group_enter(group);
[tokenImageView setImageWithURLRequest:imageRequest placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
[self.downloadedUIImages addObject:image]; //This is a mutableArray that will later be set to an UIImageView's animnationImages
dispatch_group_leave(group);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(@"Error %@", error);
dispatch_group_leave(group);
}];
}
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
// do your completion stuff here
});
关于ios - 如何判断循环中的 block 是否全部执行完毕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23253175/