由于某些原因,当我使用Firebase存储检索我的下载图像URL(我已确认该图像实际上已发送到Firebase存储)时,该URL返回nil。我很确定我的文件路径匹配/正确,但是我不确定在哪里出错。
let selectedImageData=UIImageJPEGRepresentation(selectedImage.image!, 1)
let storageRef=Storage.storage().reference()
let metadata=StorageMetadata()
storageRef.child("Users").child(Auth.auth().currentUser!.uid)
.child("Pictures"+String(self.tracker)).putData(selectedImageData!, metadata: metadata)
{
(metadata, error) in
if error != nil{
print("There was a problem uploading")
return
}
storageRef.child("Users").child(Auth.auth().currentUser!.uid)
.child("Pictures"+String(self.tracker)).downloadURL(completion:
{
(url, error) in
print("What's going on")
self.pictures.append((url?.absoluteString)!)
})
}
答案 0 :(得分:1)
使用以下代码将图片上传到存储并获取图片的返回路径
let selectedImageData=UIImageJPEGRepresentation(selectedImage.image!, 1)
self.uploadProfileImageToFirebase(data: selectedImageData)
func uploadProfileImageToFirebase(data:NSData){
let storageRef = Storage.storage().reference().child("Users").child(Auth.auth().currentUser!.uid).child("Pictures").child(String(self.tracker))
//Modify the above line as per your requirement
if data != nil {
storageRef.putData(data as Data, metadata: nil, completion: { (metadata, error) in
if(error != nil){
print(error)
return
}
// Fetch the download URL
storageRef.downloadURL { url, error in
if let error = error {
// Handle any errors
if(error != nil){
print(error)
return
}
} else {
// Get the download URL
let urlStr:String = (url?.absoluteString) ?? ""
print(urlStr)
}
}
})
}
答案 1 :(得分:0)
好像您对putData和获取downloadURL的引用在某些方面有所不同,也许跟踪器在此同时发生了变化?
为避免引用不同,让我们尝试像这样更改代码:
let selectedImageData=UIImageJPEGRepresentation(selectedImage.image!, 1)
let storageRef=Storage.storage().reference()
let metadata=StorageMetadata()
let reference = storageRef.child("Users").child(Auth.auth().currentUser!.uid).child("Pictures"+String(self.tracker))
reference.putData(selectedImageData!, metadata: metadata) { (metadata, error) in
if error != nil{
print("There was a problem uploading")
return
}
reference.downloadURL { (url, error) in
print("Download error: \(error), url: \(url)")
self.pictures.append((url?.absoluteString)!)
}
}
基本上,我们确保在两种情况下都使用相同的引用。