我的问题是我正在将图像上传到Firebase存储,但是URL并未更新到数据库中的子节点,因此我无法引用该图像。
我已经检查以确保我正确输入了数据库值,并且更新子节点似乎可以在该特定功能之外进行。我尝试添加一个调度队列,因为它是一个异步函数,但没有成功。
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
var selectedImageFromPicker: UIImage?
if let originalImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
selectedImageFromPicker = originalImage
print("The photo is \(originalImage)")
}
if let selectedImage = selectedImageFromPicker, let cell = selectedCell {
cell.imageContainer.image = selectedImage
print("My image is now \(String(describing: cell.imageContainer.image))")
guard let user = Auth.auth().currentUser else {return}
let uid = user.uid
let imageName = NSUUID().uuidString
let storageRef = Storage.storage().reference().child("\(imageName).png")
if let uploadData = cell.imageContainer.image!.pngData() {
storageRef.putData(uploadData, metadata: nil, completion:
{ (metadata, error) in
if error != nil {
print(error as Any)
return
}
// Function doesn't seem to behave properly after this line
storageRef.downloadURL(completion: { (url, error) in
if let error = error {
print(error.localizedDescription)
return
} else {
let downloadUrl = url?.absoluteString
var valueKey = ""
if cell.activeCell == "0" { valueKey = "imageOne" } else if cell.activeCell == "1" {
valueKey = "imageTwo"} else if cell.activeCell == "2" { valueKey = "imageThree"} else if cell.activeCell == "3" { valueKey = "imageFour" } else if cell.activeCell == "4" { valueKey = "imageFive" } else if cell.activeCell == "5" { valueKey = "imageSix"} else { valueKey = "imageSeven" }
print("the node is titled \(valueKey)")
self.value = [valueKey: downloadUrl] as [String : AnyObject]
print("Here it is \(self.value) for \(uid)")
}
})
self.updateImageData(uid: uid, values: self.value)
print(metadata as Any)
})
}
collectionView.reloadData()
}
dismissView()
}
func updateImageData(uid: String, values: [String: AnyObject]) {
Database.database().reference().child("profile_images").child(uid).updateChildValues(values as [AnyHashable : Any], withCompletionBlock: { (error, ref) in
if let error = error {
print("Failed to update database values with error", error.localizedDescription)
return
}
})
}
预期结果是将URL链接视为指向数据库内相应键的值。
答案 0 :(得分:0)
storageRef.downloadURL
方法会调出服务器,可能需要一些时间才能完成。您的主要代码会继续执行,而不是阻止代码并使应用对用户无响应,而是立即调用self.updateImageData(uid: uid, values: self.value)
而没有正确的URL。然后,当服务器返回正确的下载URL时,您的完成处理程序将运行并设置self.value
。
因此,所有需要下载URL的代码都应位于storageRef.downloadURL
的完成处理程序中。
类似的东西:
storageRef.downloadURL(completion: { (url, error) in
if let error = error {
print(error.localizedDescription)
return
} else {
let downloadUrl = url?.absoluteString
var valueKey = ""
if cell.activeCell == "0" { valueKey = "imageOne" } else if cell.activeCell == "1" {
valueKey = "imageTwo"} else if cell.activeCell == "2" { valueKey = "imageThree"} else if cell.activeCell == "3" { valueKey = "imageFour" } else if cell.activeCell == "4" { valueKey = "imageFive" } else if cell.activeCell == "5" { valueKey = "imageSix"} else { valueKey = "imageSeven" }
self.value = [valueKey: downloadUrl] as [String : AnyObject]
self.updateImageData(uid: uid, values: self.value)
}
})