我刚刚将Firebase存储更新到5.0.0,看起来metadata.downloadURL()
不再被识别了。 (Value of type 'StorageMetadata' has no member 'downloadURL'
)
虽然在查看文档之后仍然可以使用它:
项目已清理完毕已经重建了。
我错过了什么吗?
答案 0 :(得分:14)
你能试试吗
// Create a reference to the file you want to download
let starsRef = storageRef.child("images/stars.jpg")
// Fetch the download URL
starsRef.downloadURL { url, error in
if let error = error {
// Handle any errors
} else {
// Get the download URL for 'images/stars.jpg'
}
}
答案 1 :(得分:4)
这是我对Swift 3 / Swift 4的版本。
解释代码中发生的事情。
这与Sh_Khan的答案基本相同。但在他的例子中,用户已经知道了桶路径。在我的示例中,我们从上载任务中获取路径。这就是我引导这个问题以及我认为正在寻找metadata.downloadURL()
替代品时所期待的问题。
class StorageManagager {
private let storageReference: StorageReference
init() {
// first we create a reference to our storage
// replace the URL with your firebase URL
self.storageReference = Storage.storage().reference(forURL: "gs://MYAPP.appspot.com")
}
// MARK: - UPLOAD DATA
open func uploadData(_ data: Data, named filename: String, completion: @escaping (URL? , Error?) -> Void) {
let reference = self.storageReference.child(filename)
let metadata = StorageMetadata()
metadata.contentType = "ourType" // in my example this was "PDF"
// we create an upload task using our reference and upload the
// data using the metadata object
let uploadTask = reference.putData(data, metadata: metadata) { metadata, error in
// first we check if the error is nil
if let error = error {
completion(nil, error)
return
}
// then we check if the metadata and path exists
// if the error was nil, we expect the metadata and path to exist
// therefore if not, we return an error
guard let metadata = metadata, let path = metadata.path else {
completion(nil, NSError(domain: "core", code: 0, userInfo: [NSLocalizedDescriptionKey: "Unexpected error. Path is nil."]))
return
}
// now we get the download url using the path
// and the basic reference object (without child paths)
self.getDownloadURL(from: path, completion: completion)
}
// further we are able to use the uploadTask for example to
// to get the progress
}
// MARK: - GET DOWNLOAD URL
private func getDownloadURL(from path: String, completion: @escaping (URL?, Error?) -> Void) {
self.storageReference.child(path).downloadURL(completion: completion)
}
}
答案 2 :(得分:2)
让我们在Swift 4.2中尝试以下代码:
let imgData = UIImage.jpegData(self.imageView.image!)
let imageName = UUID().uuidString
let ref = Storage.storage().reference().child("pictures/\(imageName).jpg")
let meta = StorageMetadata()
meta.contentType = "image/jpeg"
self.uploadToCloud(data: imgData(0.5)!, ref: ref, meta: meta)
UploadToCloud方法:
` Method UploadToCloud
func uploadToCloud(data:Data, ref:StorageReference, meta:StorageMetadata) {
ref.putData(data, metadata: meta) { (metaData, error) in
if let e = error {
print("==> error: \(e.localizedDescription)")
}
else
{
ref.downloadURL(completion: { (url, error) in
print("Image URL: \((url?.absoluteString)!)")
})
}
}
}
答案 3 :(得分:1)
此问题会弹出所有语言搜索。因此对于Kotlin来说,解决方案就是下面的那种:
val photoRef = FirebaseStorage.getInstance()
.reference.child("images/stars.jpg")
// Code ommited - Do some saving - putFile
photoRef.downloadUrl.addOnSuccessListener({ uri ->
product.imageUrl = uri.toString()
})
然而,这不是一个好的解决方案。您最好保存路径,然后根据需要重新构建完整的Url。例如:
photoRef.downloadUrl.addOnSuccessListener({ uri ->
val imagePath = uri.toString()
// Save to database
})
现在,您可以稍后使用它,并且只能按需使用:
FirebaseStorage.getInstance().reference.child(product.imageUrl).downloadUrl
.addOnSuccessListener { uri ->
String imageUrl = uri.toString()
// Load in images
}
答案 4 :(得分:-1)
Auth.auth().createUser(withEmail: email, password: password) { (user, error) in
if error != nil {
print(error as Any)
return
}
guard let uid = user?.user.uid else {
return
}
self.dismiss(animated: true, completion: nil)
//Добавляем картинку в firebase. Надо добавить в Pods file pod 'Firebase/Storage' и запустить терминал
let imageName = NSUUID().uuidString
let storageRef = Storage.storage().reference()
// Create a reference to the file you want to download
let starsRef = storageRef.child("profile_images").child("\(imageName).png")
let uploadData = self.profileImageView.image?.pngData()
starsRef.putData(uploadData!, metadata: nil, completion: { (metadata, error) in
if error != nil {
print(error as Any)
}
if let profileImageUrl = metadata?.path {
let values = ["name": name, "email": email, "profileImage": profileImageUrl]
self.registerUserIntoDatabaseWithUID(uid: uid, values: values)
}
})
}
答案 5 :(得分:-4)
如果您无法将URL转换为字符串...可以尝试一下
url.absoluteString