Created
September 3, 2019 15:59
-
-
Save ShawonAshraf/f5b80c2c233117fb76d024b082b9fbf8 to your computer and use it in GitHub Desktop.
Loading image from URL 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
| @IBOutlet weak var imageView: UIImageView! | |
| func setImageToImageView() { | |
| fetchImage { (imageData) in | |
| if let data = imageData { | |
| // referenced imageView from main thread | |
| // as iOS SDK warns not to use images from | |
| // a background thread | |
| DispatchQueue.main.async { | |
| self.imageView.image = UIImage(data: data) | |
| } | |
| } else { | |
| // show as an alert if you want to | |
| print("Error loading image"); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment