Reputation: 418
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
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
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