Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save ShawonAshraf/f5b80c2c233117fb76d024b082b9fbf8 to your computer and use it in GitHub Desktop.
Loading image from URL swift
@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