Created
October 24, 2019 16:27
-
-
Save anupamchugh/ffde1cfc3c6b9d3ed2f55a4400031e8e 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
@objc func showActionSheet(sender: UIButton) | |
{ | |
let alert = UIAlertController(title: "Select Image", message: nil, preferredStyle: .actionSheet) | |
alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: { _ in | |
self.launchCamera() | |
})) | |
alert.addAction(UIAlertAction(title: "Photos Library", style: .default, handler: { _ in | |
self.showPhotosLibrary() | |
})) | |
alert.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil)) | |
switch UIDevice.current.userInterfaceIdiom { | |
case .pad: | |
alert.popoverPresentationController?.sourceView = sender | |
alert.popoverPresentationController?.sourceRect = sender.bounds | |
alert.popoverPresentationController?.permittedArrowDirections = .up | |
default: | |
break | |
} | |
self.present(alert, animated: true, completion: nil) | |
} | |
func launchCamera() | |
{ | |
if UIImagePickerController.isSourceTypeAvailable(.camera){ | |
let imagePicker = UIImagePickerController() | |
imagePicker.sourceType = .camera | |
imagePicker.delegate = self | |
self.present(imagePicker, animated: true, completion: nil) | |
} | |
else{ | |
let alert = UIAlertController(title: "Warning", message: "There's no camera.", preferredStyle: .alert) | |
alert.addAction(UIAlertAction(title: "Dismiss", style: .default, handler: nil)) | |
self.present(alert, animated: true, completion: nil) | |
} | |
} | |
func showPhotosLibrary(){ | |
let imagePicker = UIImagePickerController() | |
imagePicker.sourceType = .photoLibrary | |
imagePicker.delegate = self | |
self.present(imagePicker, animated: true, completion: nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment