why downloading image from url returns nil? [duplicate]
why downloading image from url returns nil? [duplicate]
This question already has an answer here:
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.
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
the download method is asynchronous. You are returning the result before the download process is completed. What you need is to add a completion handler to your method
– Leo Dabus
Aug 27 at 2:47
1 Answer
1
The problem with your code is that the dataTask method is asynchronous. You are returning the result before the download process is completed. What you need is to add a completion handler to your method to return the image or the error once finished:
import UIKit
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
func getImage(from url: URL, completion: @escaping (UIImage?, Error?) -> ())
print("download started:", url.absoluteString)
URLSession.shared.dataTask(with: url) data, reponse, error in
guard let data = data else
completion(nil, error)
return
print("download finished:")
completion(UIImage(data: data), nil)
.resume()
let url = URL(string: "https://i.stack.imgur.com/varL9.jpg")!
getImage(from: url) image, error in
guard let image = image else
print("error:", error ?? "")
return
print("image size:", image.size)
// use your image here and don't forget to always update the UI from the main thread
DispatchQueue.main.async
self.imageView.image = image
I used the following code but I am getting this error: The requested URL was not found on this server. I can se see the URL is on my Firebase database. What could this be?
– GreekShadow
Aug 27 at 13:04
Show your url. Have you tested it in your desktop browser? What happens?
– Leo Dabus
Aug 27 at 13:09
If I copy and paste my url in the browser it automatically downloads a jpg. This is the url -> firebasestorage.googleapis.com/v0/b/the-spot-6bdd6.appspot.com/…
– GreekShadow
Aug 27 at 14:05
Just pasted your link in my playground and it printed
"image size: (3072.0, 2304.0)n"
– Leo Dabus
Aug 27 at 14:07
"image size: (3072.0, 2304.0)n"
Try using the UIImageView extension I posted here stackoverflow.com/a/27712427/2303865
– Leo Dabus
Aug 27 at 14:10
stackoverflow.com/q/24231680/2303865
– Leo Dabus
Aug 27 at 2:45