Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (void)doThisTask {
- // For this snippet we are downloading the same in two tasks.
- NSString *imageUrl = @"http://www.something.com/pic.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(), ^{
- // do stuff with image
- _imageWithBlock.image = downloadedImage;
- });
- }];
- // You always have to start up the task!
- [getImageTask resume];
- }
Add Comment
Please, Sign In to add comment