ios - 如何在 Firebase 中使用 "StorageReference.downloadURLWithCompletion()"?

标签 ios swift firebase firebase-storage

您好,我有关于使用 Firebase 制作 ios 应用程序的问题

现在我正在使用观看 YouTube 来制作聊天应用程序

https://www.youtube.com/watch?v=MJ1r5jjdf4Q&index=4&list=PLmdU__e_zPf9uczwNhLNANvxT5mzxMiXQ

在这里

在源代码中,let imageUrl = data?.downloadURL()?.absoluteString 出现错误“‘StorageMetadata’类型的值没有成员‘downloadURL’”

我听说 Firebase 中已弃用 downloadURL(),因此文档表示使用 StorageReference.downloadURLWithCompletion() 而不是 downloadURL

但我不知道如何更改代码。

请告诉我如何更改代码。并更改代码

谢谢!

func signupEvent(){
    Auth.auth().createUser(withEmail: email.text!, password: password.text!){(user,err) in
        let uid = user?.user.uid 
        let image = UIImageJPEGRepresentation(self.imageView.image!, 0.1)

        Storage.storage().reference().child("userImage").child(uid!).putData(image!, metadata:nil, completion:{(data, error) in
            let imageUrl = data?.downloadURL()?.absoluteString
            Database.database().reference().child("users").child(uid).setValue(["name": self.name.text!, "profileImageUrl" : imageUrl])

        })

    }
}

最佳答案

您可以查看 Firebase 5 上传文件的更新文档 here ,API 已更改,因此您应该使用 StorageReference 的 downloadURL 方法。

所以你的代码变成这样:

func signupEvent(){
  Auth.auth().createUser(withEmail: email.text!, password: password.text!){(user,err) in
    let uid = user?.user.uid 
    let image = UIImageJPEGRepresentation(self.imageView.image!, 0.1)

    let storageItem = Storage.storage().reference().child("userImage").child(uid!)
    storageItem.putData(image!, metadata:nil, completion:{(data, error) in
        // Now is good to check errors
        if error != nil {
            print("Error: couldn't upload the image")
        }
        else {
            storageItem.downloadURL(completion: { (url, error) in
                if error != nil {
                    print(error!)
                    return
                }
                if url != nil {
                    let imageUrl = url!.absoluteString
                    Database.database().reference().child("users").child(uid).setValue(["name": self.name.text!, "profileImageUrl" : imageUrl])
                }
            }
        }
    })

 }
}

关于ios - 如何在 Firebase 中使用 "StorageReference.downloadURLWithCompletion()"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51578547/

相关文章:

javascript - 从 React-JS 读取和查看 Firebase 数据时出错

ios - UILabel 显示 1k 表示 int 值 1000 和 2k 表示 2000 等等

ios - 使用 NSMutableURLRequest/URLRequest 请求 http

jquery - Xcode 4.5.1、Cordova 2.2、jquery 移动

Swift - 通过输入更改 BG 颜色

ios - Sprite Kit 创建 iAdBanner

ios - 跨设备建立同步音乐流

firebase - 从 Flutter 中的 Firestore 集合中获取所有内容

ios - 在Objective-C中需要声明哪些场景关键字 “volatile”?

ios - UITabBarItem 操作不起作用