I am trying to download an image that is uploaded to my database storage and the link to the image is in my Real time database. There is no problem with the link but when I use my method to return the image from the link I am getting nil. I force wrap it because it needs to return an image for now.
This is my code:
func getImageFromUrl(url: URL) -> UIImage {
var tempImage: UIImage? = nil
print("INSIDE URL -> \(url.absoluteString)")
URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil {
print("Error on getImageFromUrl : \(error!.localizedDescription)")
return
}
print("Image data " + data.debugDescription)
DispatchQueue.main.async {
tempImage = UIImage(data: data!)!
print("TEMP IMAGE > \(String(describing: tempImage?.images![0]))")
}
}.resume()
if tempImage == nil {
print("IMAGE IS NIL!")
}
return tempImage!
}
Please let me know why my code is failing.