Set UIImageView image using a url

后端 未结 10 689
隐瞒了意图╮
隐瞒了意图╮ 2020-12-08 02:24

Is it possible to read a url to an image and set a UIImageView to the image at this url?

相关标签:
10条回答
  • 2020-12-08 03:02

    Here's an updated answer to the best answer, as imageWithContentsOfURL is no longer a method of UIImage. You'll have to use CIImage:

    NSURL *url = [NSURL URLWithString:@"http://url_goes_here.com/logo.png"];
    imageView.image = [UIImage imageWithCIImage:[CIImage imageWithContentsOfURL:url]];
    
    0 讨论(0)
  • 2020-12-08 03:03

    For such a straightforward task I would highly recommend against integrating projects such as Three20, the library is a monster and not very straightforward to get up and running.

    I would instead recommend this approach:

    NSString *imageUrl = @"http://www.foo.com/myImage.jpg";
    [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:imageUrl]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        myImageView.image = [UIImage imageWithData:data];
    }];
    

    EDIT for Swift 3.0

    let urlString = "http://www.foo.com/myImage.jpg"
    guard let url = URL(string: urlString) else { return }
    URLSession.shared.dataTask(with: url) { (data, response, error) in
        if error != nil {
            print("Failed fetching image:", error)
            return
        }
    
        guard let response = response as? HTTPURLResponse, response.statusCode == 200 else {
            print("Not a proper HTTPURLResponse or statusCode")
            return
        }
    
        DispatchQueue.main.async {
            self.myImageView.image = UIImage(data: data!)
        }
    }.resume()
    

    *EDIT for Swift 2.0

    let request = NSURLRequest(URL: NSURL(string: urlString)!)
    
    NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) -> Void in
    
        if error != nil {
            print("Failed to load image for url: \(urlString), error: \(error?.description)")
            return
        }
    
        guard let httpResponse = response as? NSHTTPURLResponse else {
            print("Not an NSHTTPURLResponse from loading url: \(urlString)")
            return
        }
    
        if httpResponse.statusCode != 200 {
            print("Bad response statusCode: \(httpResponse.statusCode) while loading url: \(urlString)")
            return
        }
    
        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            self.myImageView.image = UIImage(data: data!)
        })
    
        }.resume()
    
    0 讨论(0)
  • 2020-12-08 03:04

    Unfortunately this feature is not available as of this writing... instead you will have to implement the functionality yourself by:

    1. Downloading the data of the image
    2. Saving it or caching it somewhere (db or filesystem) and then
    3. Setting the UIImaveView to the saved structure

    Fortunately you don't have to break your head coming out with said functionality as Apple provides an example that does exactly that as part of their code samples.

    Follow the code and I'm sure you will be able to accommodate it to your needs.

    0 讨论(0)
  • 2020-12-08 03:06

    use AFNetworking category UIImageView+AFNetworking.h

     #import "UIImageView+AFNetworking.h
    
    
    [profileImageView setImageWithURL:[NSURL URLWithString:photoURL] placeholderImage:[UIImage imageNamed:@"placeholder"]];
    
    0 讨论(0)
提交回复
热议问题