如何从网址中提取图片

时间:2022-02-06 21:21:22

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)

如何提取图像? (网址只是一个例子)

5 个解决方案

#1


1  

You can try with this Async Download:

您可以尝试使用此Async下载:

    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];

#2


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.

试试这个。

#3


0  

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

首先启用App Transport Security(ATS),因为URL为http。然后使用NSURLSession API下载图像

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];

#4


0  

You can use Alamofire :

你可以使用Alamofire:

Alamofire.request(.GET, completeURLImage).response { (request, response, data, error) in
       yourImageView.image = nil
       yourImageView.image = UIImage(data: data!, scale:1)
 }

#5


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.

但如果您不限于使用第三方库,我个人建议您使用SDWebImageView,此库将帮助您下载图像并对其进行缓存。

#1


1  

You can try with this Async Download:

您可以尝试使用此Async下载:

    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];

#2


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.

试试这个。

#3


0  

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

首先启用App Transport Security(ATS),因为URL为http。然后使用NSURLSession API下载图像

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];

#4


0  

You can use Alamofire :

你可以使用Alamofire:

Alamofire.request(.GET, completeURLImage).response { (request, response, data, error) in
       yourImageView.image = nil
       yourImageView.image = UIImage(data: data!, scale:1)
 }

#5


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.

但如果您不限于使用第三方库,我个人建议您使用SDWebImageView,此库将帮助您下载图像并对其进行缓存。