Skip to content

Instantly share code, notes, and snippets.

@andresr-dev
Created April 27, 2022 15:35
Show Gist options
  • Select an option

  • Save andresr-dev/fb57ad8061886ae374a358edb190ceb0 to your computer and use it in GitHub Desktop.

Select an option

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
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