das*_*ist 13 objective-c ios objective-c-blocks parse-platform
我有一个循环设置,下载一系列图像,我将在稍后用于使用animationImages属性的动画UIImageView.我想知道我的循环中的所有块何时完成执行所以我可以开始动画,并且想知道我怎么能够告诉他们什么时候完成完成?谢谢!
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.
Run Code Online (Sandbox Code Playgroud)
编辑:有问题 Error -999
我在提供的答案中执行代码时遇到以下问题: Domain=NSURLErrorDomain Code=-999 "The operation couldn’t be completed. (NSURLErrorDomain error -999.)"
快速搜索显示,这Error -999意味着" 在上一个请求完成之前发出了另一个请求 "......由于我快速连续发出了几个请求,因此这肯定是这种情况.建议的修复建议在这里,我没有工作,因为它只会成功下载一个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");
});
Run Code Online (Sandbox Code Playgroud)
Dav*_*rry 34
创建一个调度组,在for循环中输入组,在完成块中离开该组.然后你可以使用dispatch_group_notify来找出所有块何时完成:
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
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3657 次 |
| 最近记录: |