Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- +(void)createDirForImage:(NSString *)CalendarDir {
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *pathToCalendarDir = [[paths objectAtIndex:0] stringByAppendingPathComponent:CalendarDir];
- NSError *error;
- //Does directory already exist?
- if (![[NSFileManager defaultManager] fileExistsAtPath:pathToCalendarDir]) {
- if (![[NSFileManager defaultManager] createDirectoryAtPath:pathToCalendarDir
- withIntermediateDirectories:NO
- attributes:nil
- error:&error]) {
- NSLog(@"Create directory error: %@", error);
- }
- }
- }
- + (void)downloadCalendarImage {
- // For this snippet we are downloading the same in two tasks.
- NSString *imageUrl = @"http://www.fnordware.com/superpng/pnggrad8rgb.png";
- // You always start by creating an NSURLConfiguration.
- NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
- // This creates a session using the current class as a delegate.
- NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig
- delegate:self
- delegateQueue:nil];
- //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.
- NSURLSessionDownloadTask *getImageTask = [session downloadTaskWithURL:[NSURL URLWithString:imageUrl]
- completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
- // Here you use the location variable provided in the completion handler to get a pointer to the image
- //UIImage *downloadedImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:location]];
- // Finally you could, for example, update UIImageView’s image to show the new file.
- dispatch_async(dispatch_get_main_queue(), ^{//scheduleImage.image = downloadedImage;
- });
- // This is me trying to add stuff... downloading the image to a file
- NSString *imageName = [imageUrl lastPathComponent];
- // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- // NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *pathToCalendarDir = [[paths objectAtIndex:0] stringByAppendingString:@"/CalendarFolder/"];
- UIImage *imageToSave = [UIImage imageWithData:[NSData dataWithContentsOfURL:location]];
- NSData *binaryImageData = UIImagePNGRepresentation(imageToSave);
- BOOL writeSuccess = [binaryImageData writeToFile:[pathToCalendarDir stringByAppendingPathComponent:imageName] atomically:YES];
- if (writeSuccess)
- NSLog(@"The image has been downloaded and saved.");
- else
- NSLog(@"The saving of the Calendar image was unsuccessful.");
- }];
- // You always have to start up the task!
- [getImageTask resume];
- }
Advertisement
Add Comment
Please, Sign In to add comment