代码之家  ›  专栏  ›  技术社区  ›  philfreo

iPhone:如何从url获取UIImage?

  •  15
  • philfreo  · 技术社区  · 16 年前

    如何下载图像并将其转换为UIImage?

    7 回复  |  直到 16 年前
        1
  •  41
  •   philfreo    14 年前
    NSURL *url = [NSURL URLWithString:@"http://example.com/image.jpg"];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *img = [[[UIImage alloc] initWithData:data] autorelease];
    

    但是,这不是异步的。

        2
  •  15
  •   mvds    15 年前

    Read the Apple documentation about async downloads with NSURLConnection.

    下载完成后,您可以从数据对象实例化UIImage:

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
        if (requestData)
        {
            self.image = [[[UIImage alloc] initWithData:requestData] autorelease];
        }
    }
    

    requestData image 是包含类的实例变量,以及 是保留的财产。一定要释放 形象 dealloc 课堂常规,例如使用 self.image=nil; .

        3
  •  5
  •   E. Rivera    14 年前

    异步加载确实是必须的,但如果您只需要一个简单的解决方案,也可以通过后台调用来实现。

    [self performSelectorInBackground:@selector(loadImage) withObject:nil];
    
    - (void)loadImage
    {
       NSURL * url = [NSURL URLWithString:@"http://.../....jpg"];
       NSData * data = [NSData dataWithContentsOfURL:url];
       UIImage * image = [UIImage imageWithData:data];
       if (image)
       {
           // Success use the image
           ...
       }
       else
       {
           // Failed (load an error image?)
           ...
       }
    }   
    
        4
  •  3
  •   superpuccio Kasra    10 年前

    UIImageView 只需使用 dispatch_async :

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                NSURL *url = [NSURL URLWithString:myURL];
                NSData *data = [NSData dataWithContentsOfURL:url];
                UIImage* image = [[UIImage alloc]initWithData:data];
    
                dispatch_async(dispatch_get_main_queue(), ^{
                    [myImageView setImage:image];
                });
            });
    

    第一 异步调度 异步调度 用于实际注入刚刚加载到 . 请注意,第二个 异步调度 主线 (UI线程)即,实际上,用于更新UI的线程。

        5
  •  2
  •   Liam Amster    14 年前
        6
  •  1
  •   Besi    10 年前

    extension NSURL{
        func downloadImage() -> Future<UIImage, NSError>{
    
            let promise = Promise<UIImage, NSError>()
    
            Queue.global.async{
                if let data = NSData(contentsOfURL: self){
                    if let image = UIImage(data: data){
                        promise.success(image)
                        return
                    }
                }
                promise.failure(NSError())
            }
    
            return promise.future
        }
    }
    

    这样我就可以做到:

    import BrightFutures
    
    let someURL = ...;
    someURL.downloadImage().onSuccess{ image in
      // Do something with the UIImage
    }
    

    更新

    extension NSURL{
        func downloadImage() -> UIImage?{
            if let data = NSData(contentsOfURL: self){
                return UIImage(data: data){
            }
        return nil
        }
    }
    
        7
  •  0
  •   Besi    10 年前

    这就是@philfreo在swift中的回答:

    let urlString = "http://lorempixel.com/100/100/cats/"
    let url = NSURL(string: urlString)!
    let data = NSData(contentsOfURL: url)
    let image = UIImage(data: data!)
    

    image