My application downloads image packages from the server. This is an array of direct links (20-50 files) from XML.
How can I fully download the entire set of images?
How to add a condition to cancel a full download (and delete all already downloaded files) if the application was closed with the iPhone button? (such methods are in AppDelegate, while all my download code is in some kind of downloadviewcontroller.m file)
Anything else I need to worry about when downloading multiple files? (5-10 MB total)
The code I use is not very safe in case of interruption of loading or closing the application. In the background thread, I call this method for each file:
(BOOL) loadImageFromURL:(NSString *)url withName:(NSString *)filename toFolder:(NSString *)folder { NSURL *link = [NSURL URLWithString:url]; NSFileManager *manager = [NSFileManager defaultManager]; NSString *filepath = [folder stringByAppendingPathComponent:filename]; if ([manager fileExistsAtPath:filepath]) { return YES; } else { UIImage *image = [[UIImage imageWithData:[NSData dataWithContentsOfURL:link]] retain]; NSData *data = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0f)]; if ([data length] <= 0) [image release]; return NO;
source share