Hey guys, welcome to another post, which is Get Image From URL Swift 3. So basically we will learn how to get image from URL in Swift 3 or we can say downloading images in Swift 3.
Get Image From URL Swift 3 Tutorial Video
- You can also go through this video explanation.
Get Image From URL Swift 3 Tutorial
Creating a new Project
- We will create a SingleView application again.
- So I just created a project named ImageLoadingExample. (You can create a project with any name).
Adding UIImageView
- Now from the right search UIImageView. It is a view that we can use to display images.
- Drag it to your storyboard as you can see in the below image.
- Now open assistant editor and connect this view with your ViewController.swift file. If you don’t know how to do this check Xcode Button Tutorial.
- After connection your code will be like this.
// // ViewController.swift // ImageLoadingExample // // Created by Belal Khan on 03/07/17. // Copyright © 2017 Belal Khan. All rights reserved. // import UIKit class ViewController: UIViewController { @IBOutlet weak var uiImageView: UIImageView! override func viewDidLoad() { super.viewDidLoad() } }
- Now we need an Image. So I will be using the following URL .
http://www.simplifiedtechy.net/wp-content/uploads/2017/07/simplified-techy-default.png
- If you will go to the above URL you will see the following image. You can use any URL but it must be a valid URL.
- So we have the Image URL, now lets download the image.
Fetching Image from URL
- Now to fetch the Image from the URL we will use the following code.
// // ViewController.swift // ImageLoadingExample // // Created by Belal Khan on 03/07/17. // Copyright © 2017 Belal Khan. All rights reserved. // import UIKit class ViewController: UIViewController { //URL containing the image let URL_IMAGE = URL(string: "http://www.simplifiedtechy.net/wp-content/uploads/2017/07/simplified-techy-default.png") @IBOutlet weak var uiImageView: UIImageView! override func viewDidLoad() { super.viewDidLoad() let session = URLSession(configuration: .default) //creating a dataTask let getImageFromUrl = session.dataTask(with: URL_IMAGE!) { (data, response, error) in //if there is any error if let e = error { //displaying the message print("Error Occurred: \(e)") } else { //in case of now error, checking wheather the response is nil or not if (response as? HTTPURLResponse) != nil { //checking if the response contains an image if let imageData = data { //getting the image let image = UIImage(data: imageData) //displaying the image self.uiImageView.image = image } else { print("Image file is currupted") } } else { print("No response from server") } } } //starting the download task getImageFromUrl.resume() } }
Allowing HTTP Request
- The URL we are using here is http and be default ios only allows request on https urls. So we need to allow http URLs using info.plist file.
- So open the info.plist file as source code.
- Add the following xml code just before the last closing </dict> tag.
<key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict>
- Now try running the application in simulator.
- As you can see it is working absolutely fine.
So thats it for this Get Image from URL Swift 3 Tutorial. Lets discuss in comments if you have any queries or feedbacks. And please SHARE this post if you found it useful. Thank You 🙂
nilesh sinha says
thankyou belal
David Collins says
Thanks, Belal. Another nice tutorial.
I applied this code in a table view that had images in each cell, and I got problems with images appearing very slowly, and sometimes cells having the wrong image.
After much research, I found the problem – the code updates the ImageView within the background thread.
Easy fix, update it in the main thread ..
DispatchQueue.main.async {
//displaying the image
self.uiImageView.image = image
}
Quentin says
I was struggling with that but didn’t knew swift was blocking “non-https” image !
Thank you very much for your help 😉
Nilesh says
I am trying to load image from URL which is protected with username and pass but its not downloading Image.
What extra I need to pass or exactly I should do it. Please help.