Попытка изменить изображение профиля для приложения - PullRequest
0 голосов
/ 21 марта 2020

Мне нужна помощь. Я пытаюсь изменить изображение профиля своего приложения для пользователей. После нажатия он должен изменить UIImage, отправить и сохранить в Firebase Storage. Пожалуйста, смотрите ниже код с обоих контроллеров, где он работает.

class SnapViewController: UIViewController {

    @IBOutlet weak var photo: UIImageView!
        @IBOutlet weak var captionTextView: UITextView!
        @IBOutlet weak var trendButton: UIButton!
        var SelectedImage: UIImage?

        override func viewDidLoad() {
        super.viewDidLoad()
            let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.handleSelectPhoto))
                photo.addGestureRecognizer(tapGesture)
                photo.isUserInteractionEnabled = true
        }

        @objc func handleSelectPhoto() {
                let pickerController = UIImagePickerController()
                    pickerController.delegate = self
                    pickerController.mediaTypes = ["public.image", "public.movie"]
                    present(pickerController, animated: true, completion: nil)
        }

        @IBAction func trendButton_TouchUpInside(_ sender: Any) {
                view.endEditing(true)
            ProgressHUD.show("Waiting...", interaction: false)
                if let profileImg = self.SelectedImage, let imageData = profileImg.jpegData(compressionQuality: 0.8) {
                        let photoIdString = NSUUID().uuidString
                        let storageRef = Storage.storage().reference(forURL: Config.ROOT_STORAGE_REF).child("Posts").child(photoIdString)
                        storageRef.putData(imageData, metadata: nil, completion: { (metadata, error) in
                            if error != nil {
                                return
                            }
                            //let photoUrl = url?.absoluteString
                        let photoUrl = metadata?.storageReference?.downloadURL
                            sendDataToDatabase(photoUrl: photoUrl)
                        })
                } else {
                    ProgressHUD.showError("Profile Image can't be empty")
                }
    }

    func sendDataToDatabase(photoUrl:String) {
                let ref = Database.database().reference()
                let postsReference = ref.child("posts")
            let newPostId = postsReference.childByAutoId().key!
                let newPostReference = postsReference.child(newPostId)
                newPostReference.setValue(["photoUrl": photoUrl], withCompletionBlock: {
                        (error, ref) in
                        if error != nil {
                                ProgressHUD.showError(error!.localizedDescription)
                                return
                        }
                        ProgressHUD.show("Success!")
                })
        }

        extension SnapViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
                private func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
                    print("did Finish Picking Media")
                    if let image = info["UIImagePickerControllerOriginalImage"] as? UIImage{
                    SelectedImage = image
                        photo.image = image
                }
                dismiss(animated: true, completion: nil)
            Second Snippet of Swift code:
        }

        @objc func handleSelectProfileImageView() {
                let pickerController = UIImagePickerController()
                pickerController.delegate = self
                present(pickerController, animated: true, completion: nil)
        }

        @IBAction func DismissOnClick(_ sender: Any) {
                dismiss(animated: true, completion: nil)
        }

        //Firebase Authentication for User Sign-up

        @IBAction func signUpButton_TouchUpInside(_ sender: Any) {
                view.endEditing(true)
                ProgressHUD.show("Waiting...", interaction: false)
                if let profileImg = self.selectedImage, let imageData = profileImg.jpegData(compressionQuality: 0.8) {
                        AuthService.signUp(username: usernameFieldText.text!, email: emailTextField.text!, password: passwordTextField.text!, imageData: imageData, onSuccess: {
                            ProgressHUD.showSuccess("Success")
                            self.performSegue(withIdentifier: "signUpToTabbar", sender: nil)
                            }, onError: { (errorString) in
                                ProgressHUD.showError(errorString!)
                        })
                } else {
                        ProgressHUD.showError("Profile Image can't be empty")
                }
            }
}

Extension ViewController

extension SignUpViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
        private func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
                print("did Finish Picking Media")
                    if let image = info["UIImagePickerControllerOriginalImage"] as? UIImage{
                            selectedImage = image
                            profileimage.image = image
                    }
                    dismiss(animated: true, completion: nil)
            }
    }

Буду признателен за любую помощь.

Спасибо! Angerbot

...