- Import
UIImagePickerControllerDelegate
and UINavigationControllerDelegate
dekegates in your class declaration:
class ViewController: UIViewController,
UIImagePickerControllerDelegate,
UINavigationControllerDelegate {
...
}
- Instantiate the Camera controller (usually inside a Button's action)
if UIImagePickerController.isSourceTypeAvailable(.camera) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .camera;
imagePicker.allowsEditing = false
present(imagePicker, animated: true, completion: nil)
}
- Instantiate the Photo Library (usually inside a Button's action)
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary;
imagePicker.allowsEditing = false
present(imagePicker, animated: true, completion: nil)
}
- Call the
UIImagePickerController
delegate inside the .swift
file to get a UIImage
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
myImg = image
}
dismiss(animated: true, completion: nil)
}