另一个Mac Catalyst移植问题。在iOS上完美运行的用于获取视频的AVCaptureDevice的代码失败,并且在macOS上运行时返回nil。
展示此问题的最简单方法是使用苹果公司自己的AvCam demo app。它在Swift和Objective-C中都提供。两者都以相同的方式失败。选中“ Mac”复选框,然后在Mac上构建并运行。授予对摄像机的权限,然后在控制台中注意到错误。
在Swift版本中,查看CameraViewController的configureSession
方法。行:
var defaultVideoDevice: AVCaptureDevice?
// Choose the back dual camera, if available, otherwise default to a wide angle camera.
if let dualCameraDevice = AVCaptureDevice.default(.builtInDualCamera, for: .video, position: .back) {
defaultVideoDevice = dualCameraDevice
} else if let backCameraDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back) {
// If a rear dual camera is not available, default to the rear wide angle camera.
defaultVideoDevice = backCameraDevice
} else if let frontCameraDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .front) {
// If the rear wide angle camera isn't available, default to the front wide angle camera.
defaultVideoDevice = frontCameraDevice
}
即使已授予相机许可,也始终会导致nil
。
或者查看Objective-C版本的AVCamCameraViewController类的configureSession
方法。尝试获取AVCaptureDevice的相同代码失败。
我也尝试使用AVCaptureDevice default(for:)
方法,并且尝试使用AVCaptureDevice.DiscoverySession devices
。在Mac上运行时,所有尝试都会失败,而在iOS设备上运行时,所有尝试都会失败。
更有趣的是,在与AvCam演示应用程序相同的我自己的应用程序中,我可以使用UIImagePickerController很好地拍照和捕获视频。这意味着,通常,Mac Catalyst应用程序可以使用Mac的相机拍摄照片和视频。
问题是,要获得用于视频的AVCaptureDevice,需要进行哪些更改?任何人都可以找出在Mac Catalyst上运行的AvCam演示应用程序需要进行哪些更改吗?
答案 0 :(得分:3)
Cameras and Media Capture文档页面上的此注释说明这是设计使然:
重要
在macOS中运行的iPad应用程序不能使用AVFoundation Capture类。这些应用应改为使用UIImagePickerController进行照片和视频捕获。
Apple开发者论坛上还有一个How to get the AVCam demo app to work under Mac Catalyst?线程,其中包括来自Apple支持部门的声明,表明其行为符合预期。