MyViewController
class MyViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
...
@IBOutlet weak var pictureImage: UIImageView!
let picker = UIImagePickerController()
...
override func viewDidLoad() {
super.viewDidLoad()
picker.delegate = self
...
}
...
// MARK: - Actions
@IBAction func changePictureAction(_ sender: Any) {
...
changePhoto()
}
...
func changePhoto() {
let optionMenu = UIAlertController(title: nil, message: "Lets get a picture", preferredStyle: .actionSheet)
let selectFromLibraryAction = UIAlertAction(title: "Select photo from library", style: .default, handler: {
(alert: UIAlertAction!) -> Void in
self.selectPictureFromLibrary()
})
optionMenu.addAction(selectFromLibraryAction)
if (UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)) {
let takePictureAction = UIAlertAction(title: "Take a picture", style: .default, handler: {
(alert: UIAlertAction!) -> Void in
self.selectPictureFromCamera()
})
optionMenu.addAction(takePictureAction)
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: {
(alert: UIAlertAction!) -> Void in
print("Cancelled")
})
optionMenu.addAction(cancelAction)
self.present(optionMenu, animated: true, completion: nil)
}
func selectPictureFromLibrary() {
picker.sourceType = .photoLibrary
present(picker, animated: true, completion: nil)
}
func selectPictureFromCamera() {
picker.sourceType = .camera
present(picker, animated: true, completion: nil)
}
...
// MARK: - UIImagePickerControllerDelegate
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
{
let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
pictureImage.contentMode = .scaleAspectFit
pictureImage.image = chosenImage
dismiss(animated:true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
}
Info.plist
Key: Privacy - Photo Library Usage Description
Type: String
Value: <you message>
Key: Privacy - Camera Usage Description
Type: String
Value: <you message>
No hay comentarios:
Publicar un comentario