Skip to content

Instantly share code, notes, and snippets.

@danielt1263
Last active April 12, 2017 20:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielt1263/8fe0919e9d1f5a1f7e1a97e10c5fef03 to your computer and use it in GitHub Desktop.
Save danielt1263/8fe0919e9d1f5a1f7e1a97e10c5fef03 to your computer and use it in GitHub Desktop.
class ViewController: UIViewController
{
@IBAction func changeAvatar(_ sender: UITapGestureRecognizer) {
guard let senderView = sender.view else { fatalError("Tapped on viewless gesture recognizer?") }
let controller = UIImagePickerController()
controller.delegate = self
choiceIndexUsingActionSheet(title: "", message: "", choices: sourceOptions.map { $0.title }, onSourceView: senderView).then { index in
sourceOptions[index].action(controller)
self.present(controller, animated: true, completion: nil)
}
}
}
typealias ImagePickerAction = (UIImagePickerController) -> Void
private let sourceOptions = { () -> [(title: String, action: ImagePickerAction)] in
var result = [(title: String, action: ImagePickerAction)]()
if UIImagePickerController.isSourceTypeAvailable(.camera) {
result.append(("Camera", { $0.sourceType = .camera }))
}
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
result.append(("Photos", { $0.sourceType = .photoLibrary }))
}
return result
}()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment