Swift 4 相机视图,为啥这会在 iPad 而不是 iPhone 上崩溃?
Posted
技术标签:
【中文标题】Swift 4 相机视图,为啥这会在 iPad 而不是 iPhone 上崩溃?【英文标题】:Swift 4 camera view, why does this crash on iPad and not on iPhone?Swift 4 相机视图,为什么这会在 iPad 而不是 iPhone 上崩溃? 【发布时间】:2018-11-19 21:01:16 【问题描述】:我正在为 iPad 构建一个可以拍照的应用程序。为什么这段代码在 iPad 上会崩溃,但在 iPhone 上却能正常运行?我假设它与设备功能有关?我正在使用运行 ios 12.1 和 Swift 4.1 的 iPad Pro 12.9 和 Xcode 10。我在 info.plist
中添加了有关相机隐私和照片隐私的必要行。感谢您的指导。
它在guard
语句的行处崩溃。
private func configure()
// Preset the session for taking photo in full resolution
captureSession.sessionPreset = AVCaptureSession.Preset.photo
// Get the front and back-facing camera for taking photos
let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInDualCamera], mediaType: AVMediaType.video, position: .unspecified)
for device in deviceDiscoverySession.devices
if device.position == .back
backFacingCamera = device
else if device.position == .front
frontFacingCamera = device
currentDevice = backFacingCamera
guard let captureDeviceInput = try? AVCaptureDeviceInput(device: currentDevice) else
return
// Configure the session with the output for capturing still images
stillImageOutput = AVCapturePhotoOutput()
// Configure the session with the input and the output devices
captureSession.addInput(captureDeviceInput)
captureSession.addOutput(stillImageOutput)
// Provide a camera preview
cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
view.layer.addSublayer(cameraPreviewLayer!)
cameraPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
cameraPreviewLayer?.frame = view.layer.frame
// Bring the camera button to front
view.bringSubviewToFront(cameraButton)
captureSession.startRunning()
// Toggle Camera recognizer
toggleCameraGestureRecognizer.direction = .up
toggleCameraGestureRecognizer.addTarget(self, action: #selector(toggleCamera))
view.addGestureRecognizer(toggleCameraGestureRecognizer)
// Zoom In recognizer
zoomInGestureRecognizer.direction = .right
zoomInGestureRecognizer.addTarget(self, action: #selector(zoomIn))
view.addGestureRecognizer(zoomInGestureRecognizer)
// Zoom Out recognizer
zoomOutGestureRecognizer.direction = .left
zoomOutGestureRecognizer.addTarget(self, action: #selector(zoomOut))
view.addGestureRecognizer(zoomOutGestureRecognizer)
@objc func toggleCamera()
captureSession.beginConfiguration()
// Change the device based on the current camera
guard let newDevice = (currentDevice?.position == AVCaptureDevice.Position.back) ? frontFacingCamera : backFacingCamera else
return
// Remove all inputs from the session
for input in captureSession.inputs
captureSession.removeInput(input as! AVCaptureDeviceInput)
// Change to the new input
let cameraInput:AVCaptureDeviceInput
do
cameraInput = try AVCaptureDeviceInput(device: newDevice)
catch
print(error)
return
if captureSession.canAddInput(cameraInput)
captureSession.addInput(cameraInput)
currentDevice = newDevice
captureSession.commitConfiguration()
【问题讨论】:
嘿,面临同样的问题,但在 Iphone 设备上,让我知道您的解决方案是什么 【参考方案1】:这是设备功能。 iPad 有广角摄像头。
let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: .unspecified)
【讨论】:
以上是关于Swift 4 相机视图,为啥这会在 iPad 而不是 iPhone 上崩溃?的主要内容,如果未能解决你的问题,请参考以下文章
为啥使用相机意图捕获的图像会在 Android 上的某些设备上旋转?