当数据库字典在另一个视图控制器中时,如何向Firebase数据库添加图像URL

时间:2019-07-07 19:29:33

标签: ios swift firebase

我有一个视图控制器(UserProfilePage.swift),需要在其中添加个人资料图片并将URL添加到数据库。但是将值添加到数据库的字典在另一个视图控制器(SignUpScreen.swift)中。这是我在UserProfilePage.swift中添加图像的代码:

extension UserProfilePage: UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        // To choose the edited image
        if let selectedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {

            databaseUserProfileImage = selectedImage
            userProfileImage.image = selectedImage

            // Upload image to firebase
            guard let imageData = selectedImage.jpegData(compressionQuality: 0.4) else {
                return
            }

            let storageRef = Storage.storage().reference(forURL: "gs://smallworld-89f21.appspot.com")


        } else if let selectedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
            userProfileImage.image = selectedImage
        }
        // To dismiss the picker after selecting the image
        picker.dismiss(animated: true, completion: nil)
    }

}

这是我在SignUpScreen.swift中创建字典的代码:

let values = ["Email": email, "Username": username, "Country": country, "UserID": userID, "ProfilePictureURL": ""]

如您在代码中所见,我有一个用于图像URL的密钥。从另一个视图控制器上载图像时,如何更新字典?任何帮助都将受到赞赏。谢谢!

1 个答案:

答案 0 :(得分:1)

将其设置为实例变量

var values = [String:Any]()

然后做

values["ProfilePictureURL"] =  // someValue

BTW info提供了一个UIImage实例而不是url,那么您应该转换为数据并上传