从URL目标C获取图像

时间:2009-05-31 23:25:38

标签: objective-c

我正在尝试从网址获取图片,但它似乎对我不起作用。有人能指出我正确的方向吗?

这是我的代码:

NSURL *url = [NSURL URLWithString:@"http://myurl/mypic.jpg"];

NSString *newIMAGE = [[NSString alloc] initWithContentsOfURL:url
                                       encoding:NSUTF8StringEncoding error:nil];

cell.image = [UIImage imageNamed:newIMAGE];

当我调试newIMAGE字符串为nil时,那里的东西不起作用。

5 个答案:

答案 0 :(得分:162)

你想要的是获取图像数据,然后使用该数据初始化UIImage:

NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: @"http://myurl/mypic.jpg"]];
cell.image = [UIImage imageWithData: imageData];
[imageData release];

根据要求,这是一个异步版本:

dispatch_async(dispatch_get_global_queue(0,0), ^{
    NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: @"http://myurl/mypic.jpg"]];
    if ( data == nil )
        return;
    dispatch_async(dispatch_get_main_queue(), ^{
        // WARNING: is the cell still using the same data by this point??
        cell.image = [UIImage imageWithData: data];
    });
    [data release];
});

答案 1 :(得分:4)

好的,这里有一些问题:

  1. 从URL(url)到NSString(newImage)的转换不正确,代码实际上做的是尝试将“http://myurl/mypic.jpg”的内容加载到NSString中。

    < / LI>
  2. -imageNamed方法接受一个字符串,该字符串是本地文件的路径,而不是URL作为参数。

  3. 您需要使用NSData对象作为中介,如下例所示: http://blogs.oreilly.com/digitalmedia/2008/02/creating-an-uiimage-from-a-url.html

答案 2 :(得分:3)

接受的答案异步版本在我的代码中运行得非常慢。使用NSOperation的方法工作时间更快。代码由Joe Masilotti提供 - &gt; objective - C : Loading image from URL?(并粘贴在下方):

-(void) someMethod {
    // set placeholder image
    UIImage* memberPhoto = [UIImage imageNamed:@"place_holder_image.png"];

    // retrieve image for cell in using NSOperation
    NSURL *url = [NSURL URLWithString:group.photo_link[indexPath.row]];
    [self loadImage:url];
}

- (void)loadImage:(NSURL *)imageURL
{
    NSOperationQueue *queue = [NSOperationQueue new];
    NSInvocationOperation *operation = [[NSInvocationOperation alloc]
                                        initWithTarget:self
                                        selector:@selector(requestRemoteImage:)
                                        object:imageURL];
    [queue addOperation:operation];
}

- (void)requestRemoteImage:(NSURL *)imageURL
{
    NSData *imageData = [[NSData alloc] initWithContentsOfURL:imageURL];
    UIImage *image = [[UIImage alloc] initWithData:imageData];

    [self performSelectorOnMainThread:@selector(placeImageInUI:) withObject:image waitUntilDone:YES];
}

- (void)placeImageInUI:(UIImage *)image
{
    [self.memberPhotoImage setImage:image];
}

答案 3 :(得分:0)

Swift 3和4

let theURL = URL(string:"https://exampleURL.com")
let imagedData = NSData(contentsOf: theURL!)!
let theImage = UIImage(data: imagedData as Data)
cell.theImageView.image = theImage

这将在主线程中完成。

并在异步/后台线程

中执行相同的操作
   DispatchQueue.main.async(){
      let theURL = URL(string:"https://exampleURL.com")
      let imagedData = NSData(contentsOf: theURL!)!
      let theImage = UIImage(data: imagedData as Data)
   }
   cell.theImageView.image = theImage

答案 4 :(得分:0)

根据吉姆·道维(Jim dovey)的答案,[data release]不再需要,因为在更新的Apple指南中。内存管理由ARC(自动计数参考)自动完成,

这是更新后的asynchronous call

dispatch_async(dispatch_get_global_queue(0,0), ^{
        NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: @"your_URL"]];
        if ( data == nil )
            return;
        dispatch_async(dispatch_get_main_queue(), ^{
            self.your_UIimage.image = [UIImage imageWithData: data];
        });

    });