Created
April 13, 2017 13:30
-
-
Save danielt1263/566cd1678c0ae7277bc138c999cae481 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class ViewController: UIViewController | |
{ | |
@IBOutlet weak var avatarView: UIImageView! | |
var api: API! | |
var imagePickerDelegate: ImagePickerDelegate! | |
@IBAction func changeAvatar(_ sender: UITapGestureRecognizer) { | |
guard let senderView = sender.view else { fatalError("Tapped on viewless gesture recognizer?") } | |
imagePickerDelegate = ImagePickerDelegate() | |
imagePickerDelegate.promise.then { [unowned self] image -> Promise<UIImage> in | |
guard let data = UIImageJPEGRepresentation(image, 0.8) else { throw JPEGRepresentationError.badImage } | |
return self.api.upload(avatar: data).then { image } | |
} | |
.then { [unowned self] image in | |
self.avatarView.image = image | |
} | |
.always { [unowned self] in | |
self.dismiss(animated: true, completion: nil) | |
} | |
.catch { [unowned self] error in | |
guard error as? UserInteractionError != .userCanceled else { return } | |
self.displayInformationAlert(title: "Error", message: error.localizedDescription) | |
} | |
let controller = UIImagePickerController() | |
controller.delegate = imagePickerDelegate | |
choiceIndexUsingActionSheet(title: "", message: "", choices: sourceOptions.map { $0.title }, onSourceView: senderView).then { index in | |
sourceOptions[index].action(controller) | |
self.present(controller, animated: true, completion: nil) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment