Created
September 3, 2019 15:58
-
-
Save ShawonAshraf/f1a86839b8b79243caf7384af402f65b to your computer and use it in GitHub Desktop.
Image from URL in Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func fetchImage(from urlString: String, completionHandler: @escaping (_ data: Data?) -> ()) { | |
| let session = URLSession.shared | |
| let url = URL(string: image_url) | |
| let dataTask = session.dataTask(with: url!) { (data, response, error) in | |
| if error != nil { | |
| print("Error fetching the image! 😢") | |
| completionHandler(nil) | |
| } else { | |
| completionHandler(data) | |
| } | |
| } | |
| dataTask.resume() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment