Доступ к камере не работает в приложении macCataylst, но в версии для iOS работает нормально [закрыто] ⇐ IOS
-
Anonymous
Доступ к камере не работает в приложении macCataylst, но в версии для iOS работает нормально [закрыто]
I am targeting iOS 17.4 and macOS 14.2 with a iOS/macCatalyst app. The app is providing a photo picker or access the camera to capture an image. My Signing and Capabilities has the following settings: App Sandbox - Hardware - Camera Hardware Runtime - Camera and Photo Library
The info.plist includes the Privacy - Camera Usage Description String - stating why the camera is required.
in my SwiftUI view controller I have the following code:
struct NewGreetingCardView: View { @State var frontImageSelected: Image? = Image("frontImage") @State var frontPhoto = false @State var sourceType: UIImagePickerController.SourceType = .photoLibrary @State var captureFrontImage: Bool = false @State private var cameraNotAuthorized = false @State private var isCameraPresented = false var body: some View { NavigationView { Form { // removed other sections for clarity Section("Image") { ZStack { frontImageSelected? .resizable() .aspectRatio(contentMode: .fit) .shadow(radius: 10 ) Image(systemName: "camera.fill") .foregroundColor(.white) .font(.largeTitle) .shadow(radius: 10) .frame(width: 150) .onTapGesture { self.frontPhoto = true } .actionSheet(isPresented: $frontPhoto) { () -> ActionSheet in ActionSheet( title: Text("Choose mode"), message: Text("Select one."), buttons: [ ActionSheet.Button.default(Text("Camera"), action: { checkCameraAuthorization() self.captureFrontImage.toggle() self.sourceType = .camera }), ActionSheet.Button.default(Text("Photo Library"), action: { self.captureFrontImage.toggle() self.sourceType = .photoLibrary }), ActionSheet.Button.cancel() ] ) } .sheet(isPresented: $captureFrontImage) { ImagePicker( sourceType: sourceType, image: $frontImageSelected) } } .frame(width: 300, height: 300) } } .onAppear(perform: { checkCameraAuthorization() }) .alert(isPresented: $cameraNotAuthorized) { Alert( title: Text("Unable to access the Camera"), message: Text("To enable access, go to Settings > Privacy > Camera and turn on Camera access for this app."), primaryButton: .default(Text("Settings")) { openSettings() } , secondaryButton: .cancel() ) } } } func openSettings() { #if os(macOS) SettingsLink { Text("Settings") } #else guard let settingsUrl = URL(string: UIApplication.openSettingsURLString) else { return } if UIApplication.shared.canOpenURL(settingsUrl) { UIApplication.shared.open(settingsUrl, completionHandler: { _ in }) } #endif } func checkCameraAuthorization() { var checkCamera: Bool checkCamera = AVCaptureDevice.authorizationStatus(for: .video) == .authorized if checkCamera == true { cameraNotAuthorized = false } else { cameraNotAuthorized = true } } } The code correctly prompts the user for authentication on iOS and macCataylst, but when I present the camera view frame, on macOS I get a blank screen, and the app crashes with the error: Thread 1: EXC_BAD_ACCESS(code=1, address=0x1c8) and a stack trace of
Thread 1 Queue : com.apple.main-thread (serial) #0 0x000000018107365c in class_getMethodImplementation () #1 0x000000018260aad8 in _NSKVONotifyingOriginalClassForIsa () #2 0x000000018262ad44 in _NSKeyValueObservationInfoGetObservances () #3 0x0000000182d46b10 in NSKeyValueWillChangeWithPerThreadPendingNotifications () #4 0x000000019f120c10 in -[AVCaptureFigVideoDevice _setVideoHDREnabled:forceResetVideoHDRSuspended:] () #5 0x000000019f16d2fc in -[AVCaptureSession _updateVideoHDREnabledForDevice:forceResetVideoHDRSuspended:] () #6 0x000000019f16b600 in -[AVCaptureSession _updateDeviceActiveFormatsAndActiveConnections] () #7 0x000000019f169aa0 in -[AVCaptureSession _buildAndRunGraph:] () #8 0x000000019f161684 in -[AVCaptureSession _commitConfiguration] () #9 0x000000019f161288 in -[AVCaptureSession commitConfiguration] () #10 0x000000019f12c97c in -[AVCaptureVideoPreviewLayer dealloc] () #11 0x0000000182d47898 in _NSKVOPerformWithDeallocatingObservable () #12 0x00000001826659b8 in NSKVODeallocate () #13 0x0000000189713888 in CA::Transaction::commit() () #14 0x00000001898e8bc8 in CA::Transaction::flush_as_runloop_observer(bool) () #15 0x0000000181521254 in __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ () #16 0x0000000181521140 in __CFRunLoopDoObservers () #17 0x000000018151fe58 in CFRunLoopRunSpecific () #18 0x000000018bcc0000 in RunCurrentEventLoopInMode () #19 0x000000018bcbfe3c in ReceiveNextEventCommon () #20 0x000000018bcbfb94 in _BlockUntilNextEventMatchingListInModeWithFilter () #21 0x0000000184d77970 in _DPSNextEvent () #22 0x0000000185569dec in -[NSApplication(NSEventRouting) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] () #23 0x0000000184d6acb8 in -[NSApplication run] () #24 0x0000000184d41f54 in NSApplicationMain () #25 0x0000000184f94610 in _NSApplicationMainWithInfoDictionary () #26 0x000000019a7b00dc in UINSApplicationMain () #27 0x00000001b089c778 in UIApplicationMain () #28 0x00000001ca74affc in ___lldb_unnamed_symbol178559 () #29 0x00000001ca74ae44 in ___lldb_unnamed_symbol178557 () #30 0x00000001ca3802bc in static App.main() () #31 0x0000000100cddf28 in static GreetKeeperApp.$main() () #32 0x0000000100cde090 in main at /Users/michaelrowe/Documents/TheApApp/CardTracker/CardTracker/GreetKeeperApp.swift:13 #33 0x00000001810ba0e0 in start () There are multiple messages in the debug log that have to do with CMIQHardware does not support async still capture, but I am not sure where how that fits in for a simple picture. ===== UPDATE ====== Here's the ImagePicker Code
struct ImagePicker: UIViewControllerRepresentable { var sourceType: UIImagePickerController.SourceType = .camera @Binding var image: Image? @Environment(\.presentationMode) private var presentationMode func makeCoordinator() -> Coordinator { Coordinator(self) } func makeUIViewController(context: Context) -> UIImagePickerController { let imagePicker = UIImagePickerController() imagePicker.allowsEditing = false imagePicker.sourceType = sourceType imagePicker.delegate = context.coordinator return imagePicker } func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) { // Nothing to update here } final class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate { var parent: ImagePicker init(_ parent: ImagePicker) { self.parent = parent } func imagePickerController( _ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) { if let image = info[.originalImage] as? UIImage { parent.image = Image(uiImage: image) } parent.presentationMode.wrappedValue.dismiss() } func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { parent.presentationMode.wrappedValue.dismiss() } } }``` ===== END UPDATE ====== Are there specific things that should be looked at for Mac Catalyst. In a prior version of this app, (before macOS 14) I did nothing at all special for Mac Catalyst.
Источник: https://stackoverflow.com/questions/780 ... os-version
I am targeting iOS 17.4 and macOS 14.2 with a iOS/macCatalyst app. The app is providing a photo picker or access the camera to capture an image. My Signing and Capabilities has the following settings: App Sandbox - Hardware - Camera Hardware Runtime - Camera and Photo Library
The info.plist includes the Privacy - Camera Usage Description String - stating why the camera is required.
in my SwiftUI view controller I have the following code:
struct NewGreetingCardView: View { @State var frontImageSelected: Image? = Image("frontImage") @State var frontPhoto = false @State var sourceType: UIImagePickerController.SourceType = .photoLibrary @State var captureFrontImage: Bool = false @State private var cameraNotAuthorized = false @State private var isCameraPresented = false var body: some View { NavigationView { Form { // removed other sections for clarity Section("Image") { ZStack { frontImageSelected? .resizable() .aspectRatio(contentMode: .fit) .shadow(radius: 10 ) Image(systemName: "camera.fill") .foregroundColor(.white) .font(.largeTitle) .shadow(radius: 10) .frame(width: 150) .onTapGesture { self.frontPhoto = true } .actionSheet(isPresented: $frontPhoto) { () -> ActionSheet in ActionSheet( title: Text("Choose mode"), message: Text("Select one."), buttons: [ ActionSheet.Button.default(Text("Camera"), action: { checkCameraAuthorization() self.captureFrontImage.toggle() self.sourceType = .camera }), ActionSheet.Button.default(Text("Photo Library"), action: { self.captureFrontImage.toggle() self.sourceType = .photoLibrary }), ActionSheet.Button.cancel() ] ) } .sheet(isPresented: $captureFrontImage) { ImagePicker( sourceType: sourceType, image: $frontImageSelected) } } .frame(width: 300, height: 300) } } .onAppear(perform: { checkCameraAuthorization() }) .alert(isPresented: $cameraNotAuthorized) { Alert( title: Text("Unable to access the Camera"), message: Text("To enable access, go to Settings > Privacy > Camera and turn on Camera access for this app."), primaryButton: .default(Text("Settings")) { openSettings() } , secondaryButton: .cancel() ) } } } func openSettings() { #if os(macOS) SettingsLink { Text("Settings") } #else guard let settingsUrl = URL(string: UIApplication.openSettingsURLString) else { return } if UIApplication.shared.canOpenURL(settingsUrl) { UIApplication.shared.open(settingsUrl, completionHandler: { _ in }) } #endif } func checkCameraAuthorization() { var checkCamera: Bool checkCamera = AVCaptureDevice.authorizationStatus(for: .video) == .authorized if checkCamera == true { cameraNotAuthorized = false } else { cameraNotAuthorized = true } } } The code correctly prompts the user for authentication on iOS and macCataylst, but when I present the camera view frame, on macOS I get a blank screen, and the app crashes with the error: Thread 1: EXC_BAD_ACCESS(code=1, address=0x1c8) and a stack trace of
Thread 1 Queue : com.apple.main-thread (serial) #0 0x000000018107365c in class_getMethodImplementation () #1 0x000000018260aad8 in _NSKVONotifyingOriginalClassForIsa () #2 0x000000018262ad44 in _NSKeyValueObservationInfoGetObservances () #3 0x0000000182d46b10 in NSKeyValueWillChangeWithPerThreadPendingNotifications () #4 0x000000019f120c10 in -[AVCaptureFigVideoDevice _setVideoHDREnabled:forceResetVideoHDRSuspended:] () #5 0x000000019f16d2fc in -[AVCaptureSession _updateVideoHDREnabledForDevice:forceResetVideoHDRSuspended:] () #6 0x000000019f16b600 in -[AVCaptureSession _updateDeviceActiveFormatsAndActiveConnections] () #7 0x000000019f169aa0 in -[AVCaptureSession _buildAndRunGraph:] () #8 0x000000019f161684 in -[AVCaptureSession _commitConfiguration] () #9 0x000000019f161288 in -[AVCaptureSession commitConfiguration] () #10 0x000000019f12c97c in -[AVCaptureVideoPreviewLayer dealloc] () #11 0x0000000182d47898 in _NSKVOPerformWithDeallocatingObservable () #12 0x00000001826659b8 in NSKVODeallocate () #13 0x0000000189713888 in CA::Transaction::commit() () #14 0x00000001898e8bc8 in CA::Transaction::flush_as_runloop_observer(bool) () #15 0x0000000181521254 in __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ () #16 0x0000000181521140 in __CFRunLoopDoObservers () #17 0x000000018151fe58 in CFRunLoopRunSpecific () #18 0x000000018bcc0000 in RunCurrentEventLoopInMode () #19 0x000000018bcbfe3c in ReceiveNextEventCommon () #20 0x000000018bcbfb94 in _BlockUntilNextEventMatchingListInModeWithFilter () #21 0x0000000184d77970 in _DPSNextEvent () #22 0x0000000185569dec in -[NSApplication(NSEventRouting) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] () #23 0x0000000184d6acb8 in -[NSApplication run] () #24 0x0000000184d41f54 in NSApplicationMain () #25 0x0000000184f94610 in _NSApplicationMainWithInfoDictionary () #26 0x000000019a7b00dc in UINSApplicationMain () #27 0x00000001b089c778 in UIApplicationMain () #28 0x00000001ca74affc in ___lldb_unnamed_symbol178559 () #29 0x00000001ca74ae44 in ___lldb_unnamed_symbol178557 () #30 0x00000001ca3802bc in static App.main() () #31 0x0000000100cddf28 in static GreetKeeperApp.$main() () #32 0x0000000100cde090 in main at /Users/michaelrowe/Documents/TheApApp/CardTracker/CardTracker/GreetKeeperApp.swift:13 #33 0x00000001810ba0e0 in start () There are multiple messages in the debug log that have to do with CMIQHardware does not support async still capture, but I am not sure where how that fits in for a simple picture. ===== UPDATE ====== Here's the ImagePicker Code
struct ImagePicker: UIViewControllerRepresentable { var sourceType: UIImagePickerController.SourceType = .camera @Binding var image: Image? @Environment(\.presentationMode) private var presentationMode func makeCoordinator() -> Coordinator { Coordinator(self) } func makeUIViewController(context: Context) -> UIImagePickerController { let imagePicker = UIImagePickerController() imagePicker.allowsEditing = false imagePicker.sourceType = sourceType imagePicker.delegate = context.coordinator return imagePicker } func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) { // Nothing to update here } final class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate { var parent: ImagePicker init(_ parent: ImagePicker) { self.parent = parent } func imagePickerController( _ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) { if let image = info[.originalImage] as? UIImage { parent.image = Image(uiImage: image) } parent.presentationMode.wrappedValue.dismiss() } func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { parent.presentationMode.wrappedValue.dismiss() } } }``` ===== END UPDATE ====== Are there specific things that should be looked at for Mac Catalyst. In a prior version of this app, (before macOS 14) I did nothing at all special for Mac Catalyst.
Источник: https://stackoverflow.com/questions/780 ... os-version
Мобильная версия