Load Image from URL

extension UIImageView { func load(link: String) { guard let url = URL(string: link) else { return } let session = URLSession(configuration: .default) //creating Task let getImageFromUrl = session.dataTask(with: url) { (data, response, error) in if let e = error { print("Error Occurred: \(e)") } else { if (response as? HTTPURLResponse) != nil { //Image validation if let imageData = data { let image = UIImage(data: imageData) DispatchQueue.main.async { self.image = image } } else { print("Image file is currupted") } } else { print("No response from server") } } } getImageFromUrl.resume() } }
Remember the App Transprt Security on Info.plist to allow external connections

1 Response

sdfsdf

Write a comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.