31 lines
1.3 KiB
Swift
31 lines
1.3 KiB
Swift
// SPDX-FileCopyrightText: 2026 Kaede Akino
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
import SwiftUI
|
|
import UIKit
|
|
|
|
/// UIKit is used solely for the system camera; composing remains native SwiftUI.
|
|
struct MobileAssistantCamera: UIViewControllerRepresentable {
|
|
let finished: (Data?) -> Void
|
|
func makeCoordinator() -> Coordinator { Coordinator(finished: finished) }
|
|
func makeUIViewController(context: Context) -> UIImagePickerController {
|
|
let picker = UIImagePickerController()
|
|
picker.sourceType = .camera
|
|
picker.mediaTypes = ["public.image"]
|
|
picker.delegate = context.coordinator
|
|
return picker
|
|
}
|
|
func updateUIViewController(_ controller: UIImagePickerController, context: Context) {}
|
|
final class Coordinator: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
|
|
let finished: (Data?) -> Void
|
|
init(finished: @escaping (Data?) -> Void) { self.finished = finished }
|
|
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { finished(nil) }
|
|
func imagePickerController(
|
|
_ picker: UIImagePickerController,
|
|
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]
|
|
) {
|
|
finished((info[.originalImage] as? UIImage)?.jpegData(compressionQuality: 0.9))
|
|
}
|
|
}
|
|
}
|