All checks were successful
OpenClimb Docker Deploy / build-and-push (push) Successful in 2m13s
55 lines
1.6 KiB
Swift
55 lines
1.6 KiB
Swift
import SwiftUI
|
|
import UIKit
|
|
|
|
struct CameraImagePicker: UIViewControllerRepresentable {
|
|
@Binding var isPresented: Bool
|
|
let onImageCaptured: (UIImage) -> Void
|
|
|
|
func makeUIViewController(context: Context) -> UIImagePickerController {
|
|
let picker = UIImagePickerController()
|
|
picker.delegate = context.coordinator
|
|
picker.sourceType = .camera
|
|
picker.cameraCaptureMode = .photo
|
|
picker.cameraDevice = .rear
|
|
picker.allowsEditing = false
|
|
return picker
|
|
}
|
|
|
|
func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) {
|
|
// Nothing here actually... Q_Q
|
|
}
|
|
|
|
func makeCoordinator() -> Coordinator {
|
|
Coordinator(self)
|
|
}
|
|
|
|
class Coordinator: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
|
|
let parent: CameraImagePicker
|
|
|
|
init(_ parent: CameraImagePicker) {
|
|
self.parent = parent
|
|
}
|
|
|
|
func imagePickerController(
|
|
_ picker: UIImagePickerController,
|
|
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]
|
|
) {
|
|
if let image = info[.originalImage] as? UIImage {
|
|
parent.onImageCaptured(image)
|
|
}
|
|
parent.isPresented = false
|
|
}
|
|
|
|
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
|
|
parent.isPresented = false
|
|
}
|
|
}
|
|
}
|
|
|
|
// Extension to check camera availability
|
|
extension CameraImagePicker {
|
|
static var isCameraAvailable: Bool {
|
|
UIImagePickerController.isSourceTypeAvailable(.camera)
|
|
}
|
|
}
|