Guest User

ImageDownload.m

a guest
Feb 22nd, 2013
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  ImageDownload.m
  3. //
  4.  
  5. #import "ImageDownload.h"
  6.  
  7. @implementation ImageDownload {
  8.     NSMutableArray *downloableImages;
  9.     int position;
  10.    
  11.     mySync *mySync;
  12. }
  13.  
  14. - (void)start:(NSArray *)images sync:(mySync *)sync
  15. {
  16.     NSArray *imageData;
  17.     int imageDataCount;
  18.    
  19.     mySync = sync;
  20.     downloableImages = [[NSMutableArray alloc] init];
  21.    
  22.     for (NSArray *image in images) {
  23.         imageData = [image valueForKey:@"image"];
  24.         imageDataCount = [imageData count];
  25.        
  26.         if (imageDataCount > 0) {
  27.             [downloableImages addObject:imageData];
  28.         }
  29.     }
  30.    
  31.     [self downloadAllFiles];
  32. }
  33.  
  34. - (void)downloadAllFiles
  35. {
  36.     NSString *filename;
  37.     NSString *fileUrl;
  38.    
  39.     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  40.     NSString *cacheDirectory = [paths objectAtIndex:0];
  41.    
  42.     position = 0;
  43.     int count = [downloableImages count];
  44.  
  45.     NSLog(@"Files to download %d", count);
  46.    
  47.     for (NSArray *image in downloableImages) {
  48.         filename = [image objectAtIndex:0];
  49.         fileUrl = [image objectAtIndex:1];
  50.            
  51.         NSURL *url = [NSURL URLWithString:fileUrl];
  52.         NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:url];
  53.            
  54.         NSOperationQueue *queue = [[NSOperationQueue alloc] init];
  55.         [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
  56.             position++;
  57.            
  58.             float progress = ((float)position/(float)count);
  59.             NSLog(@"%f", progress);
  60.             NSLog(@"%@", fileUrl);
  61.            
  62.            
  63.             if ([data length] > 0 && [[NSString stringWithFormat:@"%@", error] isEqualToString:@"(null)"]) {
  64.                 UIImage *downloadedImage = [[UIImage alloc] initWithData:[NSData dataWithData:data]];
  65.                 NSString *imageDestinationPath = [NSString stringWithFormat:@"%@%@", cacheDirectory, filename];
  66.                
  67.                 NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(downloadedImage, 100.0)];
  68.                 [imageData writeToFile:imageDestinationPath atomically:YES];
  69.             } else if ([data length] == 0 && [[NSString stringWithFormat:@"%@", error] isEqualToString:@"(null)"]) {
  70.                 NSLog(@"No data");
  71.             } else if (![[NSString stringWithFormat:@"%@", error] isEqualToString:@"(null)"]) {
  72.                 NSLog(@"Error = %@", error);
  73.             }
  74.            
  75.             NSLog(@" ");
  76.            
  77.             if (position == count) {
  78.                 NSLog(@"one download complete...");
  79.                
  80.                 [mySync setProgressImageDownload:1.0];
  81.                 [mySync downloadImagesComplete];
  82.             } else {
  83.                 [mySync setProgressImageDownload:progress];
  84.             }
  85.         }];
  86.     }
  87. }
  88.  
  89. @end
Add Comment
Please, Sign In to add comment