通常情况下,我使用url将图片更新为firebase存储:
Storage.storage().reference().child("profile_images").child(fileName).putData(uploadData, metadata: nil) { [weak self] (metadata, err) in
guard let strongSelf = self else { return }
if let err = err {
print(err)
}
guard let profileImageUrl = metadata?.downloadURL()?.absoluteString else { return }
ProfilePhotoHandler.Instance.addPhotoUrl(withUrl: profileImageUrl)
}
但是现在Xcode 9.3显示警告:downloadURL()'不推荐使用:使用`StorageReference.downloadURLWithCompletion()
Firebase文档仍然显示旧方式。现在有人能帮我解决这个问题吗?
答案 0 :(得分:3)
随着swift 5.0的更改,元数据不再具有downloadURL方法。
相反,您必须执行以下操作:
let storageRef = Storage.storage().reference().child("message_images").child(fileName)
storageRef.putData(uploadData, metadata: nil) { (metadata, err) in
if let err = err {
print(err)
}
storageRef.downloadURL(completion: { (url, error) in
if error != nil {
print("Failed to download url:", error!)
return
} else {
//Do something with url
}
})
})
}
答案 1 :(得分:0)
我遇到了同样的问题,但是我使用以下代码修复了该问题:
uploadTask.observe(.success) { snapshot in
guard let imageURL = snapshot.metadata?.storageReference?.downloadURL(completion: { (url, error) in if error != nil {
print(error as Any)
} else {
}
}) else { return }
let imageStr = String(describing: imageURL)
DBService.manager.updatePhoto(profileImageUrl: imageStr)
AuthService.manager.updatePhoto(urlString: imageStr)
}
}