Reputation: 1091
I am doing multiple downloads using ASIHTTPRequest. Asynchronous download method is using here, so that multiple downloads can be performed. It is working fine. Now I want to save these downloading files into the application's local folder(in the device), and also want to retrieve it (want to the get the path to each files). How can I do this? And as here multiple downloads are performing how can I differentiate each files in the downloads? Here is the code that I have used.
- (void)download{
UIImageView *image = (UIImageView *)[mainView viewWithTag:selectedTag];
for (UIProgressView *currentProgress in [image subviews]) {
if ([currentProgress isKindOfClass:[UIProgressView class]]) {
NSLog(@"Prog tag: %d",currentProgress.tag);
if(currentProgress)
{
currentProgress.progress = 0.0;
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:[urlArray objectAtIndex:selectedTag]]];
[request setDownloadProgressDelegate:currentProgress];
[request setShowAccurateProgress:YES];
[request setDownloadDestinationPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"mansory_porsche_carrera_3-1920x1200.jpg"]];
[request shouldContinueWhenAppEntersBackground];
[request allowResumeForFileDownloads];
[request startAsynchronous];
}
}
}
}
Upvotes: 1
Views: 1989
Reputation: 5800
First of all, decide wether you want to use blocks or to use delegate functions.
If you want to use blocks, then since ASIHTTPRequest is a subclass of NSOperation, you can use -setComplectionBlock, to create a callback that will be called when this particular request has finished downloading
If you want to use delegate functions, then set the delegate of each request to your desired object (you controller that may be) and then implement in the same object -requestFinished and -requestFailed so that you can handle the responses.
Each time a url has finished downloading, the callback will be called with the request that downloaded the file as an argument. This request has the -responseData object, that when the request is finished, it will be populated with the data downloaded. This will be your image data.
This is the normal and most know way. Now, you are doing it directly it seems, but you have set each request to write on the same file on the disk! Are you sure you will get the data you want from multiple images this way? It seems to me that image data will be overlapped while downloading!
Here is what I would do: I would download each image, and while having downloaded each one, I would use a callback function to rename the resulting file. After that, from inside the same callback I would store the actual file name inside an NSMutableDictionary in my controller. Something like [myDict setValue:"path/to/image.png" forKey:"myImage1"] so that I can access them later.
<< On a side note, I think you should abandon AIHTTPRequest, as it has been abandoned but the developer. You can use AFNetworking as a great alternative. It also provides progress callbacks, and is block-based. If you want pre-iOS 4.0 compatibility, please ignore the last paragraph. >>
Upvotes: 1
Reputation: 688
You can modify these functions accordingly and can save any file u want.
- (NSString *)applicationDocumentsDirectory
{
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}
- (void)saveImage:(UIImage*)image:(NSString*)imageName
{
NSData *imageData = UIImagePNGRepresentation(image); //convert image into .png format.
NSFileManager *fileManager = [NSFileManager defaultManager];//create instance of NSFileManager
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it
NSString *documentsDirectory = [paths objectAtIndex:0]; //create NSString object, that holds our exact path to the documents directory
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]]; //add our image to the path
[fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; //finally save the path (image)
NSLog(@"image saved");
}
And for retrieving u can use these lines to check if the file exists or not:
NSString *cachedImageFileUrl = [documentsDirectory stringByAppendingPathComponent:yourFileName];
NSFileManager *fileManager = [NSFileManager defaultManager];
if([fileManager fileExistsAtPath])
{
//File exists...
}
else
{
}
Tell me if it helps!!!
Upvotes: 1