Created
April 27, 2022 15:35
-
-
Save andresr-dev/fb57ad8061886ae374a358edb190ceb0 to your computer and use it in GitHub Desktop.
This is how you can open the camera of your iPhone to take a picture from your App
This file contains hidden or 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
| import SwiftUI | |
| struct CameraManager: UIViewControllerRepresentable { | |
| @Binding var image: UIImage? | |
| class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate { | |
| let parent: PictureManager | |
| init(_ parent: PictureManager) { | |
| self.parent = parent | |
| } | |
| func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { | |
| picker.dismiss(animated: true) | |
| guard let image = info[.editedImage] as? UIImage else { | |
| print("No image found") | |
| return | |
| } | |
| parent.image = image | |
| } | |
| } | |
| func makeUIViewController(context: Context) -> UIImagePickerController { | |
| let picker = UIImagePickerController() | |
| picker.sourceType = .camera | |
| picker.allowsEditing = true | |
| picker.delegate = context.coordinator | |
| return picker | |
| } | |
| func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) { | |
| } | |
| func makeCoordinator() -> Coordinator { | |
| Coordinator(self) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment