Created
April 12, 2017 18:52
-
-
Save danielt1263/2fc623a05be007df879e5fe065a7239b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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