如何从网址中提取图片

时间:2016-12-15 05:51:45

标签: ios objective-c image url uiimageview

我正在尝试从网址中提取图片并将其放在图片视图中。

这是我的代码,但它不起作用:

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;

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

5 个答案:

答案 0 :(得分:1)

您可以尝试使用此异步下载:

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

答案 1 :(得分: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];

以异步模式显示图像,

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

试试这个。

答案 2 :(得分:0)

如果不想使用第三方库

,这是正常方法
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;  
});  
});

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

答案 3 :(得分:0)

首先启用App Transport Security(ATS),因为网址为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)

你可以使用Alamofire:

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