Last active
April 22, 2022 15:10
-
-
Save fisherds/9d6b76a2ff5bfe8ce42352b7d45ce846 to your computer and use it in GitHub Desktop.
Swift code to manual load an image view with a URL (spoiler alert we'll replace this with the Kingfisher pod)
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 load(imageView: UIImageView, from url: String) { | |
| if let imgUrl = URL(string: url) { | |
| DispatchQueue.global().async { // Download in the background | |
| do { | |
| let data = try Data(contentsOf: imgUrl) | |
| DispatchQueue.main.async { // Then update on main thread | |
| imageView.image = UIImage(data: data) | |
| } | |
| } catch { | |
| print("Error downloading image: \(error)") | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment