Brian,
I'm trying to update the user's profile image.
We can do this with createProfileChangeRequest().
I can store the image we select in the UIImagePickerController to storage.
I'm also setting the value we get from the ImageUrl, but it doesn't update in our user's database structure please tell me what to do.
I also set a button in the userprofile header and then set the delegate to be able to call this function in our userprofile controller.
I'm not sure if it's because when we call the userprofile it's a customimageview, it doesn't work. Please help.
func didtapImage() {
print("from userprofile controller")
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
imagePickerController.allowsEditing = true
present(imagePickerController, animated: true) {
}
}
@objc 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 functions as above
dismiss(animated: true, completion: nil)
}