[英]url coming back from firebase storage is nil Swift4
I'm trying to upload an image to firebase storage and get the download url to save it in database, but I get a nil value and the function returns upon checking it. 我正在尝试将图像上传到firebase存储并获取下载URL以将其保存在数据库中,但是我得到一个nil值并且该函数在检查时返回。 I followed the documentation and solutions from other posts and I can't see where I'm mistaking. 我按照其他帖子的文档和解决方案,我看不出我在哪里错。 The function is : 功能是:
func uploadImage(completed: @escaping (Bool) -> (),_ image: UIImage ){
print(" ############## UPLOAD STARTED ###########")
// let stringURL: String?
// guard let uid = Auth.auth().currentUser?.uid else {return} // use the userUid to sign the alert
// Create a root reference
let storageRef = Storage.storage().reference()
// Create a reference to "mountains.jpg"
// let alertsRef = storageRef.child("userAlertImages.jpg")//("user/\(uid)") // change path for userAlertImages path
// Create a reference to 'images/mountains.jpg'
let alertsImagesRef = storageRef.child("Alert Images/userAlertImages.jpg")
// While the file names are the same, the references point to different files
// alertsRef.name == alertsImagesRef.name; // true
// alertsRef.fullPath == alertsImagesRef.fullPath; // false
let imageData = UIImageJPEGRepresentation(image, 0.5)
let metaData = StorageMetadata()
metaData.contentType = " jpeg " // data type
metaData.customMetadata = ["k1": "",
"k2" : " ",
"k3" : "",
"k4" : ""]
alertsImagesRef.putData(imageData! as Data , metadata: metaData) { metaData, error in
if(error != nil){
print(error as Any)
return
}
}
// Fetch the download URL
alertsImagesRef.downloadURL { (url,error) in
guard let downloadURL = url else {
print("@@@@@@@@@@ downloaded url is: \(url) @@@@@@@@@@@@@")
return
}
NewMapViewController.alertImageURL = (url?.absoluteString) ?? ""
// NewMapViewController.alertImageURL = (downloadURL)
print("######### url is:\(String(describing: url)) #########")
completed(true)
// self.postAlertNotification()
self.tapCounter = 0
self.performSegue(withIdentifier: "chooseIconSegue", sender: self)
}
}
can you see where's the error? 你能看出错误在哪里吗? Many thanks. 非常感谢。
Thinking about it I realised that downloadURL
fetch was actually done before than the image upload was complete, that's because Firebase is asynchronous. 考虑到这一点,我意识到downloadURL
fetch实际上是在图像上传完成之前完成的,这是因为Firebase是异步的。 So I added a completion block
to the uploading part, and in the completion scope
I put the downloadURL
fetching part. 所以我在上传部分添加了一个completion block
,在completion scope
我放了downloadURL
获取部分。 It's not blinking eyes fast and I will appreciate any suggestion on speeding up things, because this way there is a little lag before the segue
gets performed. 它并没有快速眨眼,我会欣赏任何关于加速事情的建议,因为这样在segue
执行之前会有一点滞后。 It's not annoying at all an I could, and probably should, add a little spinning wheel to show users that the app has not frozen, but I rather just avoid the lag altogether if possible. 这根本不是烦人的,我可以,并且可能应该添加一个小轮子向用户显示该应用程序尚未冻结,但我宁愿尽可能避免延迟。 I left the prints
written in hope that this post will help others new to Firebase as I am, giving detailed almost step-by-step guidance, as this kind of answers really helped me before, and I haven't found any on the subject. 我留下了所写的prints
,希望这篇文章能像我一样帮助其他新人加入Firebase,提供详细的逐步指导,因为这种答案真的对我有帮助,而且我没有找到任何关于这个主题的内容。 。 Rewritten function is: 改写的功能是:
func uploadImage(completed: @escaping (Bool) -> (),_ image: UIImage ){
print(" ############## UPLOAD STARTED ###########")
// Create a root reference
let storageRef = Storage.storage().reference()
// Create a reference to Images folder
let alertsImagesRef = storageRef.child("Alert Images")
// Create a reference for new images
let uuid = UUID()
let imageRef = alertsImagesRef.child("userAlertImage \(uuid).jpg")
let imageData = UIImageJPEGRepresentation(image, 0.5)
let metaData = StorageMetadata()
metaData.contentType = " jpeg " // data type
metaData.customMetadata = ["k1": "",
"k2" : " ",
"k3" : "",
"k4" : ""]
imageRef.putData(imageData! as Data , metadata: metaData, completion: { metaData, error in
if(error != nil){
print(error as Any)
return
}
print(" ####### image uploaded #######")
self.tapCounter = 0
self.performSegue(withIdentifier: "chooseIconSegue", sender: self)
// fetch url v2
imageRef.downloadURL(completion: { (url, err) in
if let err = err {
print("Error downloading image file, \(err.localizedDescription)")
return
}
guard let url = url else { return }
//Now you have the download URL for the image, and can do whatever you want with it.
NewMapViewController.alertImageURL = url.absoluteString
print(" ######### url is:\(String(describing: url)) #########")
completed(true)
// self.postAlertNotification()
// self.tapCounter = 0
// self.performSegue(withIdentifier: "chooseIconSegue", sender: self)
print(" ############## UPLOAD ENDED ###########")
})
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.