我正在实现一个代码,用于下载图像并将其保存在应用程序的数据库中,
我有一个对象数组,每个对象包含图像url和一些其他信息。要下载图像,我正在使用类库AFImageRequestOperation.h AFNetworking。
我的代码下载并保存数据库中的数据,但需要通知用户下载了哪个图像,例如:如果我有一个包含5个对象的数组(在每个对象的上方引用),则必须下载数组中的顺序相同,但是当AFImageRequestOperation使异步下载时,可以在第一个项目之前下载第4项。
简而言之,我想拥有控制权,只有在完成上一次下载后才能下次下载。
我有一个for通过对象数组运行并为每个位置调用一个函数,该函数具有以下代码:
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[arrImagem valueForKey:@"urlimagem"]]];
AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:request imageProcessingBlock:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image){
Imagens *imagem = (Imagens *)[NSEntityDescription insertNewObjectForEntityForName:@"IMAGENS" inManagedObjectContext:managedObjectContext];
// Save Image
NSData *imageData = UIImageJPEGRepresentation(image, 90);
[imagem setCategoria:cat];
[imagem setTitulo:[arrImagem valueForKey:@"titulo"]];
[imagem setDescricao:[arrImagem valueForKey:@"descricao"]];
[imagem setImagem:imageData];
NSError *error;
if(![managedObjectContext save:&error]){
NSLog(@"houve um erro muito grave");
//return false;
}else{
NSLog(@"Salvou imagem");
}
}failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){
NSLog(@"%@", [error localizedDescription]);
}];
[operation start];
我不知道我的问题是否非常清楚,但基本上我的问题与此类似link
答案 0 :(得分:0)
AFImageRequestOperation是NSOperation的子类,因此您可以使用:
- (void) addDependency: (NSOperation*) operation
确保一个操作在另一个操作之前完成。
例如:
NSOperation *op1 = [[NSOperation alloc]init];
NSOperation *op2 = [[NSOperation alloc]init];
[op1 addDependency: op2];
这样 op1 就会在 op2 完成之前启动。
答案 1 :(得分:0)
您可以在类中创建一个方法,您可以在其中调用下载代码并添加一个块作为参数,该块接收下载的UIImage,图像URL(以及您需要的其他信息),或者您可以使用相同的参数实现委托来自街区。它可以锁定类似的东西:
-(void)downloadImageWithSuccess:(void (^)(UIImage *image, NSString *url, OtherParms here))success
failure:(void (^)(NSError *error)failure {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[arrImagem valueForKey:@"urlimagem"]]];
AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:request imageProcessingBlock:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image){
Imagens *imagem = (Imagens *)[NSEntityDescription insertNewObjectForEntityForName:@"IMAGENS" inManagedObjectContext:managedObjectContext];
// Save Image
NSData *imageData = UIImageJPEGRepresentation(image, 90);
[imagem setCategoria:cat];
[imagem setTitulo:[arrImagem valueForKey:@"titulo"]];
[imagem setDescricao:[arrImagem valueForKey:@"descricao"]];
[imagem setImagem:imageData];
NSError *error;
if(![managedObjectContext save:&error]){
NSLog(@"houve um erro muito grave");
//return false;
}else{
NSLog(@"Salvou imagem");
}
success(imagem, [request.URL absoluteString], otherParams);
}failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){
NSLog(@"%@", [error localizedDescription]);
failure(error);
}];
[operation start];
}
}
当你从代码中调用方法时,你可以做一些事情(使用块):
[catalog downloadImageWithSuccess:^(UIImage *image, NSString *url, OtherParms here) {
//NOTIFY USER THAT THE IMAGE WITH URL HAS BEEN DOWNLOADED
}
failure:^(NSError *error) {
//NTOFIY USER THAT THE IMAGE FAILED
}
];
答案 2 :(得分:0)
设法解决了我的问题,抱歉发布解决方案的延迟,请按照以下代码:
dispatch_group_t group = dispatch_group_create();
dispatch_group_enter(group);
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:strUrl]];
AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:request imageProcessingBlock:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image){
//your implementation
dispatch_group_leave(group); //<== NOTICE THIS
}failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){
//your implementation
NSLog(@"%@", [error localizedDescription]);
}];
[operation start];
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
因此按顺序下载ocorerão,如果此代码在一个case中,则只有在方法成功^:或者失败^:被调用时才会调用下一个下载