0

What do I have to write in else body to retrieve image from firebase storage and display it in UI?

public func downloadFile(url: String){

    var img: Image?

    let storage = Storage.storage()
    let storageRef = storage.reference()
    let imageRef = storageRef.child("Wallpapers/Cars/Wallpaper1.jpg")
    imageRef.downloadURL { (url, err) in
        if let err = err{
            print("Error! Unable to download")
        }
        else{

        }
    }

}

2 Answers2

5

For swift 5, you can use this :

let Ref = Storage.storage().reference(forURL: yourUrl)
Ref.getData(maxSize: 1 * 1024 * 1024) { data, error in
    if error != nil {
        print("Error: Image could not download!")
    } else {
        yourImageView.image = UIImage(data: data!)
    }
}

Hope it helps...

Picode
  • 1,140
  • 1
  • 6
  • 12
1

This could be a way:

// Create a reference to the file you want to download
let storageRef = FirebaseStorage.StorageReference()
let imgsRef    = storageRef.child(imgURL)

// Fetch the download URL
imgsRef.downloadURL { url, error in
    if error != nil {
        // Handle error
    } else {
        // Get the image
        URLSession.shared.dataTask(with: url!) { data, response, error in
             guard let data = data, let image = UIImage(data: data) else { return }
            RunLoop.main.perform {
                 self.initialImage = image
            }
        }.resume()
    }
}
Marcos
  • 461
  • 6
  • 10