Last active
April 12, 2017 20:30
-
-
Save danielt1263/8fe0919e9d1f5a1f7e1a97e10c5fef03 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
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