如何从 SwiftUI 或 Swift 中的 firebase 存储下载或检索图像 5

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

我必须在 else body 中写什么才能从 firebase 存储中检索图像并将其显示在 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{

        }
    }

}

对于swift5,你可以使用这个:

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

希望对您有所帮助...

这可能是一种方式:

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