Last Updated: February 25, 2016
·
610
· diegocaridei

Download image

It is an easy snippet for to download image from the web

NSString *imageUrl = @"http://someHost.com/image.jpg";
            NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
            NSURLSession *session =
            [NSURLSession sessionWithConfiguration:sessionConfig
                                          delegate:self delegateQueue:nil];
            NSURLSessionDownloadTask *getImageTask =
            [session downloadTaskWithURL:[NSURL URLWithString:imageUrl]
                       completionHandler:^(NSURL *location, NSURLResponse *response,
                                           NSError *error) {

                          downloadedImage = [UIImage imageWithData:
                                                       [NSData dataWithContentsOfURL:location]];
                           dispatch_async(dispatch_get_main_queue(), ^{ // do stuff with
                                //_image is an imageVIew
                               _image.image = downloadedImage;
                           }); }];
            [getImageTask resume];