redribben

revisedDownload

Oct 26th, 2014
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. NSURLSessionDownloadTask *getImageTask = [session downloadTaskWithURL:[NSURL URLWithString:imageUrl]
  2.                                                     completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
  3.                                                         // Here you use the location variable provided in the completion handler to get a pointer to the image
  4.                                                         UIImage *downloadedImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:location]];
  5.                                                         // Finally you could, for example, update UIImageView’s image to show the new file.
  6.                                                         dispatch_async(dispatch_get_main_queue(), ^{scheduleImage.image = downloadedImage;});
  7.                                                        
  8.                                                         // This is me trying to add stuff... downloading the image to a file
  9.                                                         NSString *imageName = [@"http://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png" lastPathComponent];
  10.                                                        
  11.                                                        
  12.                                                         NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  13.                                                         NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
  14.                                                        
  15.                                                         UIImage *imageToSave = [UIImage imageWithData:[NSData dataWithContentsOfURL:location]];
  16.                                                         NSData *binaryImageData = UIImagePNGRepresentation(imageToSave);
  17.                                                        
  18.                                                         [binaryImageData writeToFile:[basePath stringByAppendingPathComponent:imageName] atomically:YES];
  19.  
  20.                                                     }];
  21. // You always have to start up the task!
  22. [getImageTask resume];
  23. }
Add Comment
Please, Sign In to add comment