【问题标题】:Uploading Image of current user to Firebase将当前用户的图片上传到 Firebase
【发布时间】:2019-05-27 22:56:16
【问题描述】:

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: 尝试从对象 [1] 插入 nil 对象”

试图让当前用户上传他的图片到存储 它在测试时给了我这个错误

func userUpdatingInfo () {
    let storageRef = Storage.storage().reference()
    if let uploadData = self.userProfileImage.image?.pngData() {
        storageRef.putData(uploadData, metadata: nil) { (meta, error) in
            if (error != nil) {
                print(error ?? "Error in uploading Image")
                return
            } else {
                print(meta!)
            }
        }
    }
}

【问题讨论】:

    标签: ios swift xcode


    【解决方案1】:

    您需要获取子参考,因为您不能直接将文件保存到您的 Firebase:https://firebase.google.com/docs/storage/ios/upload-files#create_a_reference

    通过您的用户 ID 在您的存储中有一个引用,假设您在某处具有用户的 id 属性,我将更新答案,您需要以某种方式区分子引用

    试试下面的更新代码:

    func userUpdatingInfo () {
        let currentID = Auth.auth().currentUser?.uid
        let storageRef = Storage.storage().reference().child(currentID!)
        let uploadData = self.userProfileImage.image?.pngData()
        storageRef.putData(uploadData!, metadata: nil) { (meta, error) in
                if (error != nil) {
                    print(error ?? "Error in uploading Image")
                    return
                } else {
                    print(meta!)
                    return
                }
            }
    
    }
    

    【讨论】:

    • 相同的问题 @emrepun 因为声明已更改为 pngData() 或 jpgeData() Initializer for conditional binding must have Optional type, not '(CGFloat) -> Data?
    • 我更新了我的答案,你需要以某种方式在你的存储中创建一个孩子,否则你不能直接存储在你的存储中,试试上面这个,使用某种用户标识符。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-14
    • 2018-03-12
    • 2020-12-05
    • 1970-01-01
    • 2021-03-19
    相关资源
    最近更新 更多