Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set UIImageView image using a url

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

like image 665
some_id Avatar asked Feb 10 '11 20:02

some_id


People also ask

How do I download an image from a URL?

Click on the Download Image from URL button, the field will appear on the right. Enter the full web address of the image. Click on the arrow to the right of the field and select the Force Check checkbox. Then click the Save button.

How do I get an image in Swift?

Select New > Project... from Xcode's File menu and choose the Single View App template from the iOS > Application section. Name the project Images and set User Interface to Storyboard. Leave the checkboxes at the bottom unchecked. Tell Xcode where you would like to save the project and click the Create button.

What is the difference between UIImage and UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage .


1 Answers

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() 
like image 167
noobular Avatar answered Oct 03 '22 17:10

noobular