Reputation: 346
as seen as ASIHTTP has been deprecated , for a new project i'm working on i'm trying to learn AFNetworking but i'm facing some problems i'm not able to solve. i have the need to download many images from my website to save it locally. Unfortunately this images are not server standalone by the server but are incorporated into a plist that , with the image data returns to me some parameters
the php code that serves the image is this
$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));
to manage the request to this page of the server i've used this code
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]);
}
}*/
}];
But i'm not able to save the image during the progress block ( as because i have no other parameters to the block ) and iterate trough the operation array may be a little memory consuming, in case images are really heavy or there are a lot of pages. i've read that someone suggest to use the
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:@"download.zip" append:NO];
but in this case i have no direct stream of the image but a value contained in an plist. Does anyone faced with this problem and how solved it ?
Upvotes: 0
Views: 474
Reputation: 19544
You can set completionBlock
on each of the operations to do whatever you need, whether that's saving the responseData
somewhere, or what have you.
Note that there is no guarantee that the completion blocks for all operations will be finished by the time that the batch completes, so don't depend on that in the batch completion block.
Upvotes: 1
Reputation: 1735
// 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.
Upvotes: 0