0

I am trying to extract an image from a url and place it in an image view.

Here is my code, but it doesn't work:

NSURL *imageURL = [NSURL URLWithString:@"http://www.huffingtonpost.com/entry/china-trump-sovereignity_us_5852000ee4b02edd4115d99b"];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];
_imageView.image = image;

How do I extract the image? (the url is only an example)

Elizabeth429
  • 25
  • 1
  • 7

5 Answers5

1

You can try with this Async Download:

    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];
    NSURL *urlPic = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.huffingtonpost.com/entry/china-trump-sovereignity_us_5852000ee4b02edd4115d99b"]];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:urlPic cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:timeOutTime];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

        if(!error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                UIImage *image = [UIImage imageWithData:data];                
            });
        } else {
           //Fail
        }
    }];
    [task resume];
Pushkraj Lanjekar
  • 2,254
  • 1
  • 21
  • 34
Dheeraj D
  • 4,386
  • 4
  • 20
  • 34
0
NSString *path = @"http://www.huffingtonpost.com/entry/china-trump-sovereignity_us_5852000ee4b02edd4115d99b";
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
[self.imageview setImage:image];

Show image in Async mode,

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://myurl.com/"];

    NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (data) {
            UIImage *image = [UIImage imageWithData:data];
            if (image) {
                dispatch_async(dispatch_get_main_queue(), ^{

                        imageView.image = image;
                });
            }
        }
    }];

Try this one.

dahiya_boy
  • 9,298
  • 1
  • 30
  • 51
0

Here is the Normal approach if don't wanted to go with Third Party Library

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);  
dispatch_async(queue, ^{  
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.huffingtonpost.com/entry/china-trump-sovereignity_us_5852000ee4b02edd4115d99b"]];  
UIImage *image = [UIImage imageWithData:data];  
dispatch_async(dispatch_get_main_queue(), ^{  
self.imageView.image = image;  
});  
});

but if you are not limited to use Third party Library i would personally suggest you to use SDWebImageView, This Library will help you to download image and cache it.

ChintaN -Maddy- Ramani
  • 5,156
  • 1
  • 27
  • 48
Devang Tandel
  • 2,988
  • 1
  • 21
  • 43
0

First enable App Transport Security (ATS) as the URL is http. Then use NSURLSession API to download image

NSURL *url = [NSURL URLWithString:
              @"http://www.huffingtonpost.com/entry/china-trump-sovereignity_us_5852000ee4b02edd4115d99b"];

typeof(self) __weak weakSelf = self;

NSURLSessionDownloadTask *downloadTask = [[NSURLSession sharedSession] downloadTaskWithURL:url completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {

          if (error == nil) {
              UIImage *downloadedImage = [UIImage imageWithData:
                                          [NSData dataWithContentsOfURL:location]];
              if (downloadedImage != nil) {
                  dispatch_async(dispatch_get_main_queue(), ^{
                      weakSelf.imgView.image = downloadedImage;
                  });
              }
          }
 }];

[downloadTask resume];
Community
  • 1
  • 1
Suhit Patil
  • 11,748
  • 3
  • 50
  • 60
0

You can use Alamofire :

Alamofire.request(.GET, completeURLImage).response { (request, response, data, error) in
       yourImageView.image = nil
       yourImageView.image = UIImage(data: data!, scale:1)
 }
Sarabjit Singh
  • 1,814
  • 1
  • 17
  • 28