Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ImageDownload.m
- //
- #import "ImageDownload.h"
- @implementation ImageDownload {
- NSMutableArray *downloableImages;
- int position;
- mySync *mySync;
- }
- - (void)start:(NSArray *)images sync:(mySync *)sync
- {
- NSArray *imageData;
- int imageDataCount;
- mySync = sync;
- downloableImages = [[NSMutableArray alloc] init];
- for (NSArray *image in images) {
- imageData = [image valueForKey:@"image"];
- imageDataCount = [imageData count];
- if (imageDataCount > 0) {
- [downloableImages addObject:imageData];
- }
- }
- [self downloadAllFiles];
- }
- - (void)downloadAllFiles
- {
- NSString *filename;
- NSString *fileUrl;
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
- NSString *cacheDirectory = [paths objectAtIndex:0];
- position = 0;
- int count = [downloableImages count];
- NSLog(@"Files to download %d", count);
- for (NSArray *image in downloableImages) {
- filename = [image objectAtIndex:0];
- fileUrl = [image objectAtIndex:1];
- NSURL *url = [NSURL URLWithString:fileUrl];
- NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:url];
- NSOperationQueue *queue = [[NSOperationQueue alloc] init];
- [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
- position++;
- float progress = ((float)position/(float)count);
- NSLog(@"%f", progress);
- NSLog(@"%@", fileUrl);
- if ([data length] > 0 && [[NSString stringWithFormat:@"%@", error] isEqualToString:@"(null)"]) {
- UIImage *downloadedImage = [[UIImage alloc] initWithData:[NSData dataWithData:data]];
- NSString *imageDestinationPath = [NSString stringWithFormat:@"%@%@", cacheDirectory, filename];
- NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(downloadedImage, 100.0)];
- [imageData writeToFile:imageDestinationPath atomically:YES];
- } else if ([data length] == 0 && [[NSString stringWithFormat:@"%@", error] isEqualToString:@"(null)"]) {
- NSLog(@"No data");
- } else if (![[NSString stringWithFormat:@"%@", error] isEqualToString:@"(null)"]) {
- NSLog(@"Error = %@", error);
- }
- NSLog(@" ");
- if (position == count) {
- NSLog(@"one download complete...");
- [mySync setProgressImageDownload:1.0];
- [mySync downloadImagesComplete];
- } else {
- [mySync setProgressImageDownload:progress];
- }
- }];
- }
- }
- @end
Add Comment
Please, Sign In to add comment