загрузка изображения в swift 4.2 с помощью imagePickerController - PullRequest
0 голосов
/ 05 января 2019

Я хотел бы загрузить изображение с ImagePickerController в Rails Backend, используя в Swift 4.2, используя Alamofire. Проблема, с которой я сталкиваюсь, заключается в том, что изображение должно загружаться только при нажатии кнопки регистрации. Ниже моя функция imagePickerController. Часть Alamofire должна быть сделана в функции handleRegister. Вопрос в том, как сохранить выбранное изображение в локальном хранилище устройства (userDefaults) и использовать загружаемое изображение Alamofire в функции handleRegister?

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    if let image = info[.originalImage] as? UIImage {
        registrationViewModel.bindableImage.value = image
        let imageData = image.jpegData(compressionQuality: 0.5)

        Alamofire.upload(multipartFormData: { (multipartFormData) in
            multipartFormData.append(imageData!, withName: "account[user_attributes[profile_picture]]", fileName: "user_profile_picture.png", mimeType: "image/jpeg"); for(key, value) in self.parameters {
                multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
            }
        }, to: URL_REGISTER,
           headers: nil)
        {(result) in
            switch result {
            case .success(let upload, _, _):
                upload.uploadProgress(closure: {(Progress) in
                    print("Upload progress: \(Progress.fractionCompleted)")
                })
                upload.responseJSON { response in
                    if let JSON = response.result.value {
                        print("JSON: \(JSON)")
                    }
                }
            case .failure(let encodingError):
                print(encodingError)
            }
        }
    } else {
    }
    dismiss(animated: true, completion: nil)
}




@objc fileprivate func handleRegister() {
    self.handleTapDismiss()
    guard let username = userNameTextField.text else {return}
    guard let email = emailTextField.text else {return}
    guard let password = passwordTextField.text else {return}

    registeringHUD.textLabel.text = "Register"
    registeringHUD.show(in: view)


    AuthService.instance.registerUser(email: email, password: password, username: username) { (success) in
        if success {
            print("user has signed up")

            // only upload images to backend once you are authorized

        } else {
            self.showHUDWithError()
        }
        self.dismiss(animated: true, completion: nil)
    }

}
...