0

我正在实现一个下载图像并将它们保存在应用程序数据库中的代码,

我有一个对象数组,每个对象都包含图像 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];

我不知道我的问题是否很清楚,但基本上我的问题类似于此链接

4

3 回答 3

0

AFImageRequestOperation 是 NSOperation 的子类,因此您可以使用:

- (void) addDependency: (NSOperation*) operation

以确保一个操作在另一个操作之前完成。

例如:

NSOperation *op1 = [[NSOperation alloc]init];
NSOperation *op2 = [[NSOperation alloc]init];

[op1 addDependency: op2];

这样op1在op2完成之前不会启动。

于 2013-05-13T07:56:09.843 回答
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
            }

];
于 2013-05-13T08:43:27.857 回答
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内部,则仅在调用方法success^:或failure^:时才会调用下一次下载

于 2013-05-17T20:44:10.423 回答