Sheikh Wahab Mahmood
Sheikh Wahab Mahmood

Reputation: 418

How to download or retrieve image from firebase storage in SwiftUI or Swift 5

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{

        }
    }

}

Upvotes: 0

Views: 3106

Answers (2)

Marcos
Marcos

Reputation: 471

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()
    }
}

Upvotes: 1

Picode
Picode

Reputation: 1158

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...

Upvotes: 5

Related Questions