使用Firebase createProfileChangeRequest()更新配置文件映像

时间:2018-07-19 17:07:37

标签: swift firebase uiimagepickercontroller

因此,我目前正在尝试在用户创建个人资料后更新我的用户个人资料图片。 这是代码。

它将选择的图像保存到存储中,但是我的数据库保持不变。 profileimageurl完全不变。

如何设置它才能起作用?

我想念什么?

在collectionView单元格中保存我的个人资料图像也有什么关系吗?

func didtapImage() {
    print("from userprofile controller")


    let imagePickerController = UIImagePickerController()
    imagePickerController.delegate = self
    imagePickerController.allowsEditing = true
    present(imagePickerController, animated: true) {

    }


}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    print("123")

    if let editedImage = info["UIImagePickerControllerEditedImage"] as? UIImage {
        print("what's going on here")

        let image = editedImage
         guard let uploadData = UIImageJPEGRepresentation(image, 0.3) else {return}
        let filename = NSUUID().uuidString
        let storageItem = Storage.storage().reference().child("profile_Image").child(filename)

        storageItem.putData(uploadData, metadata: nil) { (metadata, err) in
            if let err = err {
                print("failed to upload", err)
                return
            }
            storageItem.downloadURL(completion: { (downloadURL, err) in
                guard let profileImageUrl = downloadURL?.absoluteString else {return}
                print("successfully uploaded profile image:", profileImageUrl)
                guard let currentUser = Auth.auth().currentUser?.createProfileChangeRequest() else {return}

                let dictionaryValues = ["profileImageUrl": profileImageUrl]
                let values = [currentUser: dictionaryValues]

                currentUser.photoURL = URL(fileURLWithPath: profileImageUrl)
                currentUser.commitChanges(completion: { (err) in
                    if let err = err {
                        print("not updated error", err)
                        guard let uid = Auth.auth().currentUser?.uid else {return}
                        let ref = Database.database().reference().child("users").child(uid)
                        ref.setValue(values, withCompletionBlock: { (err, ref) in
                            print("changedImage")
                            if let err = err {
                                print("did not get image",err)
                            }
                            print("updated user's profile image")
                        })
                    }
                    print("updated user image", profileImageUrl)
                    self.collectionView?.reloadData()
                })
            })
        }




    } else if let originalImage = info["UIImagePickerControllerOriginalImage"] as? UIImage {

        let image = originalImage
       //same thing as above.
}

0 个答案:

没有答案