Skip to content

Instantly share code, notes, and snippets.

@ShawonAshraf
Created September 3, 2019 15:58
Show Gist options
  • Select an option

  • Save ShawonAshraf/f1a86839b8b79243caf7384af402f65b to your computer and use it in GitHub Desktop.

Select an option

Save ShawonAshraf/f1a86839b8b79243caf7384af402f65b to your computer and use it in GitHub Desktop.
Image from URL in Swift
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