Swift 类型为'的值;存储元数据&x27;没有成员';下载URL';
我刚刚将Firebase存储更新为5.0.0,看起来像是Swift 类型为'的值;存储元数据&x27;没有成员';下载URL';,swift,xcode,firebase,swift4,firebase-storage,Swift,Xcode,Firebase,Swift4,Firebase Storage,我刚刚将Firebase存储更新为5.0.0,看起来像是元数据。downloadURL()不再被识别。(类型“StorageMetadata”的值没有成员“downloadURL”) 尽管查阅了文档后,仍应提供: 项目已清理并重建 我错过什么了吗 你能试试吗 // Create a reference to the file you want to download let starsRef = storageRef.child("images/stars.jpg") // Fetch th
元数据。downloadURL()
不再被识别。(类型“StorageMetadata”的值没有成员“downloadURL”
)
尽管查阅了文档后,仍应提供:
项目已清理并重建
我错过什么了吗 你能试试吗
// 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'
}
}
这是我的Swift 3/Swift 4版本 解释代码中发生了什么 这基本上与施胡汗的答案相同。但在他的示例中,用户已经知道了bucket路径。在我的示例中,我们从上载任务获取路径。这就是导致我提出这个问题的原因,我认为op在寻找
metadata.downloadURL()
replacement时也在寻找这个问题
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)
}
}
所有语言搜索都会弹出此问题。因此,对于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
}
如果您在将URL转换为字符串时遇到困难。。。你可以试试这个
url.absoluteString
让我们在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)
上传到云方法:
` 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)!)")
})
}
}
}
现在看来这是唯一的解决办法。虽然我不明白为什么不能再从StorageMetadata实例访问URL。文档中说它将是
storageRef.downloadURL
而不是starsRef.downloadURL
,但对我来说没有任何意义,它应该是starsRef。?firebaser这里的下载URL曾经包含在任务的元数据中,但这会对性能产生影响,而不是所有用户都需要它。这就是为什么它现在总是需要单独调用,这样只有需要下载URL的用户才能看到性能影响。@FrankvanPuffelen谢谢,应该在starsRef
或storageRef
上调用它吗starsRef
更有意义,但文档中说contrary@DavidSeek的回答证实了.downloadURL
确实应该在starsRef
上调用,而不是像文档中所说的那样调用storageRef
,这对我了解您的答案更有意义。为了将其与@Sh_Khan的答案联系起来,这表明.downloadURL
应该真正在starsRef
上调用,而不是像文档中所说的storageRef
,这对我来说更有意义。当我尝试这种方法时,我会发现Firebase尚未配置的错误。我在appDelegate中有它,并且在添加init之前一直工作良好。有什么想法吗?请加上解释。
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)
}
})
}