Skip to content

Instantly share code, notes, and snippets.

@danielt1263
Created April 12, 2017 18:52
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save danielt1263/2fc623a05be007df879e5fe065a7239b to your computer and use it in GitHub Desktop.
extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image = (info[UIImagePickerControllerEditedImage] as? UIImage) ?? (info[UIImagePickerControllerOriginalImage] as? UIImage)
if let data = image.flatMap({ UIImageJPEGRepresentation($0, 0.8)} ) {
api.upload(avatar: data).then { [weak self] in
self?.avatarView.image = image
self?.dismiss(animated: true, completion: nil)
}
.catch { [weak self] error in
self?.dismiss(animated: true) {
let alert = UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self?.present(alert, animated: true, completion: nil)
}
}
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment