redribben

folder and file

Oct 28th, 2014
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. +(void)createDirForImage:(NSString *)CalendarDir {
  2.     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  3.     NSString *pathToCalendarDir = [[paths objectAtIndex:0] stringByAppendingPathComponent:CalendarDir];
  4.     NSError *error;
  5.     //Does directory already exist?
  6.     if (![[NSFileManager defaultManager] fileExistsAtPath:pathToCalendarDir]) {
  7.         if (![[NSFileManager defaultManager] createDirectoryAtPath:pathToCalendarDir
  8.                                        withIntermediateDirectories:NO
  9.                                                         attributes:nil
  10.                                                              error:&error]) {
  11.             NSLog(@"Create directory error: %@", error);
  12.         }
  13.     }
  14.    
  15. }
  16.  
  17. + (void)downloadCalendarImage {
  18.     // For this snippet we are downloading the same in two tasks.
  19.     NSString *imageUrl = @"http://www.fnordware.com/superpng/pnggrad8rgb.png";
  20.     // You always start by creating an NSURLConfiguration.
  21.     NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
  22.     // This creates a session using the current class as a delegate.
  23.     NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig
  24.                                                           delegate:self
  25.                                                      delegateQueue:nil];
  26.     //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.
  27.     NSURLSessionDownloadTask *getImageTask = [session downloadTaskWithURL:[NSURL URLWithString:imageUrl]
  28.                                                         completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
  29.                                                             // Here you use the location variable provided in the completion handler to get a pointer to the image
  30.                                                             //UIImage *downloadedImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:location]];
  31.                                                             // Finally you could, for example, update UIImageView’s image to show the new file.
  32.                                                             dispatch_async(dispatch_get_main_queue(), ^{//scheduleImage.image = downloadedImage;
  33.                                                             });
  34.                                                            
  35.                                                             // This is me trying to add stuff... downloading the image to a file
  36.                                                             NSString *imageName = [imageUrl lastPathComponent];
  37.                                                            
  38.                                                             //  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  39.                                                            // NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
  40.                                                             NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  41.                                                             NSString *pathToCalendarDir = [[paths objectAtIndex:0] stringByAppendingString:@"/CalendarFolder/"];
  42.                                                            
  43.                                                            
  44.                                                             UIImage *imageToSave = [UIImage imageWithData:[NSData dataWithContentsOfURL:location]];
  45.                                                             NSData *binaryImageData = UIImagePNGRepresentation(imageToSave);
  46.                                                            
  47.                                                             BOOL writeSuccess = [binaryImageData writeToFile:[pathToCalendarDir stringByAppendingPathComponent:imageName] atomically:YES];
  48.                                                            
  49.                                                             if (writeSuccess)
  50.                                                                 NSLog(@"The image has been downloaded and saved.");
  51.                                                             else
  52.                                                                 NSLog(@"The saving of the Calendar image was unsuccessful.");
  53.                                                         }];
  54.     // You always have to start up the task!
  55.     [getImageTask resume];
  56. }
Advertisement
Add Comment
Please, Sign In to add comment