redribben

BMDirectorySetUp.m

Oct 29th, 2014
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  BMDirectorySetUp.m
  3. //  Радио СВЕТ
  4. //
  5. //  Created by Bogdan Michalchuk on 10/27/14.
  6. //  Copyright (c) 2014 Радио CBET. All rights reserved.
  7. //
  8.  
  9. #import "BMDirectorySetUp.h"
  10.  
  11. @interface BMDirectorySetUp ()
  12.  
  13. @property (copy, nonatomic) NSString *nameOfFile;
  14.  
  15. @end
  16.  
  17. @implementation BMDirectorySetUp
  18.  
  19.  
  20. +(void)createDirForImage:(NSString *)CalendarDir {
  21.     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  22.     NSString *pathToCalendarDir = [[paths objectAtIndex:0] stringByAppendingPathComponent:CalendarDir];
  23.     NSError *error;
  24.     //Does directory already exist?
  25.     if (![[NSFileManager defaultManager] fileExistsAtPath:pathToCalendarDir]) {
  26.         if (![[NSFileManager defaultManager] createDirectoryAtPath:pathToCalendarDir
  27.                                        withIntermediateDirectories:NO
  28.                                                         attributes:nil
  29.                                                              error:&error]) {
  30.             NSLog(@"Create directory error: %@", error);
  31.         }
  32.     }
  33.    
  34. }
  35.  
  36. + (void)fetchJSON {
  37.     NSURL *JSONLocation = [NSURL URLWithString:@"http://radiolight.us/Calendar/dirList.php"];
  38.     NSString *pictureLocation = [NSString stringWithFormat:@"http://radiolight.us/Calendar/"];
  39.    
  40.     [[[NSURLSession sharedSession] dataTaskWithURL:JSONLocation
  41.                                  completionHandler:^(NSData *JSONdata, NSURLResponse *response, NSError *error) {
  42.         // Process Data
  43.                                      NSError *jsonParsingError = nil;
  44.                                      NSArray *filesInDirectory = [NSJSONSerialization JSONObjectWithData:JSONdata
  45.                                                                                                options:0
  46.                                                                                                  error:&jsonParsingError];
  47.                                      
  48.                                      for (NSDictionary *dictionary in filesInDirectory) { NSString *fileName = dictionary[@"name"];
  49.                                          if ([[fileName pathExtension] isEqualToString: @"png"]) {
  50.                                              NSString *downloadedImageName = [pictureLocation stringByAppendingPathComponent:fileName];
  51.                                              self.nameOfFile = downloadedImageName;
  52.                                          }
  53.                                          NSLog(@"Dictionary Deal finished, nameOfFile is now %d", self.nameOfFile);
  54.                                      }
  55.     }] resume];
  56. }
  57.  
  58. + (void)downloadCalendarImage {
  59.     // For this snippet we are downloading the same in two tasks.
  60.     NSString *imageUrl = @"http://www.radiolight.us/Calendar/ScreenPM.png";
  61.     // You always start by creating an NSURLConfiguration.
  62.     NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
  63.     // This creates a session using the current class as a delegate.
  64.     NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig
  65.                                                           delegate:self
  66.                                                      delegateQueue:nil];
  67.     //Tasks are always created by sessions. This one is created with the block-based method. Remember you could still use the NSURLSessionDownloadDelegate to track download progress.
  68.     NSURLSessionDownloadTask *getImageTask = [session downloadTaskWithURL:[NSURL URLWithString:imageUrl]
  69.                                                         completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
  70.                                                             // Here you use the location variable provided in the completion handler to get a pointer to the image
  71.                                                             //UIImage *downloadedImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:location]];
  72.                                                             // Finally you could, for example, update UIImageView’s image to show the new file.
  73.                                                             dispatch_async(dispatch_get_main_queue(), ^{//scheduleImage.image = downloadedImage;
  74.                                                             });
  75.                                                            
  76.                                                             // This is me trying to add stuff... downloading the image to a file
  77.                                                             NSString *imageName = [imageUrl lastPathComponent];
  78.                                                            
  79.                                                             //  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  80.                                                            // NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
  81.                                                             NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  82.                                                             NSString *pathToCalendarDir = [[paths objectAtIndex:0] stringByAppendingString:@"/CalendarFolder/"];
  83.                                                            
  84.                                                            
  85.                                                             UIImage *imageToSave = [UIImage imageWithData:[NSData dataWithContentsOfURL:location]];
  86.                                                             NSData *binaryImageData = UIImagePNGRepresentation(imageToSave);
  87.                                                            
  88.                                                             BOOL writeSuccess = [binaryImageData writeToFile:[pathToCalendarDir stringByAppendingPathComponent:imageName] atomically:YES];
  89.                                                            
  90.                                                             if (writeSuccess)
  91.                                                                 NSLog(@"The image has been downloaded and saved.");
  92.                                                             else
  93.                                                                 NSLog(@"The saving of the Calendar image was unsuccessful.");
  94.                                                         }];
  95.     // You always have to start up the task!
  96.     [getImageTask resume];
  97. }
  98.  
  99.  
  100. // if you needed to track the download progress, for either task creation method, you would need to use the following:
  101. -(void)URLSession:(NSURLSession *)session
  102.      downloadTask:(NSURLSessionDownloadTask *)downloadTask
  103.      didWriteData:(int64_t)bytesWritten
  104. totalBytesWritten:(int64_t)totalBytesWritten
  105. totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
  106. {
  107.     NSLog(@"%f / %f", (double)totalBytesWritten,
  108.           (double)totalBytesExpectedToWrite);
  109. }
  110.  
  111.  
  112. @end
Advertisement
Add Comment
Please, Sign In to add comment