0

正如 ASIHTTP 已被弃用一样,对于我正在从事的一个新项目,我正在尝试学习 AFNetworking,但我面临一些我无法解决的问题。我需要从我的网站下载许多图像以将其保存在本地。不幸的是,这些图像不是服务器独立的服务器,而是合并到一个 plist 中,图像数据返回给我一些参数

提供图像的 php 代码是这样的

$plist = new CFPropertyList();
$plist->add($maindict = new CFdictionary());
$maindict->add('foldername',new CFString($fol_name));
$file_ext = ($fol_name == "iPhoneRetina") ? "@2x.png" : ".png";
$file_path = "1";
$imgfile = __DIR__.'/documents/'.$file_path.'/'.$fol_name.'/doc_'.$file_path.'_pagina_'.$page_nr.$file_ext;
$imgbinary = fread(fopen($imgfile, "r"), filesize($imgfile));
$maindict->add('image',new CFData($imgbinary));
$maindict->add('page_nr',new CFNumber($page_nr));
$maindict->add('document_id',new CFNumber($doc_id));

管理对服务器此页面的请求,我使用了此代码

AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:downloadURL];
    NSDictionary *params;
    AFHTTPRequestOperation *operation;
    for (int x = 0; x < 10; x++) {
        params = @{
            @"page_nr" : [NSNumber numberWithInt:x],
            @"doc_id" : @1,
        };
        NSMutableURLRequest *request = [client requestWithMethod:@"POST" path:@"/getMagazinePage.php" parameters:params];
        operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
        [tempOperations addObject:operation];
    }
    [client enqueueBatchOfHTTPRequestOperations:tempOperations
                                  progressBlock:^(NSUInteger numberOfCompletedOperations, NSUInteger totalNumberOfOperations) {
                                      dispatch_async(dispatch_get_main_queue(), ^{
                                          [cell setDownloadProgress:(float)numberOfCompletedOperations/(float)totalNumberOfOperations];
                                      });

                                  } completionBlock:^(NSArray *operations) {
                                      [cell setDownloaded:YES];
/*                                      for (AFHTTPRequestOperation *ro in operations) {

                                          if (ro.error) {

                                              NSLog(@"++++++++++++++ Operation error");

                                          }else {
                                              //NSLog(@"Operation OK: %@", [ro.responseData description]);
                                          }
                                      }*/

                                  }];

但是我无法在进度块期间保存图像(因为我没有其他参数到块)并且遍历操作数组可能会消耗一些内存,以防图像真的很重或有很多页数。我读过有人建议使用

operation.outputStream = [NSOutputStream outputStreamToFileAtPath:@"download.zip" append:NO];

但在这种情况下,我没有直接的图像流,而是 plist 中包含的值。有没有人面临这个问题以及如何解决它?

4

2 回答 2

1

您可以设置completionBlock每个操作来执行您需要的任何操作,无论是保存responseData某处还是您拥有什么。

请注意,不能保证所有操作的完成块将在批处理完成时完成,因此不要依赖批处理完成块中的那个。

于 2012-11-09T18:52:59.520 回答
0
// You need this line to download the file directly to the path above, as you pointed out
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:filePath append:NO];

// Within the completion block, you can have some thing like
NSString *contentDisposition = [[operation.response allHeaderFields] objectForKey:@"Content-Disposition"];

and within contentDisposition string you will have the file name etc. That way, you will know the file name to save. 
于 2012-11-07T15:05:35.093 回答