Reputation: 69
Have the image location as gs://olio-ae400.appspot.com/Listings/Food/-M3g8pZDGmApicUAQtOi/MainImage
I want to download from this firebase storage location to imageview. Have used below code,but the unable to download image from url?
let storage = Storage.storage()
var reference: StorageReference!
reference = storage.reference(forURL: "gs://olio-ae400.appspot.com/Listings/Food/-M3g8pZDGmApicUAQtOi/MainImage")
reference.downloadURL { (url, error) in
print("image url is",url!)
let data = NSData(contentsOf: url!)
let image = UIImage(data: data! as Data)
self.img.image = image
}
Getting error at downloadURL line while retrieving the url for it in the response.
What is the correct way for it to download the image?
Upvotes: 0
Views: 3482
Reputation: 173
For Swift 5.6 and Xcode 13.4
I opened name "Images" folder in Firebase and uploaded my image which name "Corleone" in jpg format.
Firstly I fetch image data then assigned to imageView. Lastly, i downloaded image URL. You can use two method differently.
You can copy all what i wrote and past your project inside of viewDidload or buttonAction.
let storage = Storage.storage().reference().child("Images/Corleone.jpg")
storage.getData(maxSize: 1 * 1024 * 1024) { data, error in
if error != nil {
print(error?.localizedDescription ?? "errror")
}else{
let image = UIImage(data: data!)
self.imageView.image = image
storage.downloadURL { url, error in
if error != nil {
print(error?.localizedDescription ?? "error")
}else {
print(url ?? "url") //https://firebasestorage.googleapis.com/v0/b/epeycompare.appspot.com/o/Images%2FCorleone.jpg?alt=media&token=04c6369d-8036-4aef-8052-bac21c89eeda
}
}
}
}
Upvotes: 0
Reputation: 69
Install the pod firebaseUI.
import FirebaseUI
fetch the reference from the storage and set the reference to the imageview directly using SDwebimage as shown below.
let ref2 = Storage.storage().reference(forURL: "gs://hungry-aaf15.appspot.com/Listings/Food/-MV04bNvewyGPHMYUkK9/MainImage")
cell.img.sd_setImage(with: ref2)
Upvotes: 2
Reputation: 2571
Specify path and it's extension, then Use:
let path = "Listings/Food/-M3g8pZDGmApicUAQtOi/MainImage.jpg"
let reference = Storage.storage().reference(withPath: path)
reference.getData(maxSize: (1 * 1024 * 1024)) { (data, error) in
if let err = error {
print(err)
} else {
if let image = data {
let myImage: UIImage! = UIImage(data: image)
// Use Image
}
}
}
Upvotes: 1