Ios 仅在iPhone 7之前的设备上使用前置摄像头时崩溃

Ios 仅在iPhone 7之前的设备上使用前置摄像头时崩溃,ios,swift,avfoundation,avcapturesession,Ios,Swift,Avfoundation,Avcapturesession,我最近开始在基于摄像头的应用程序上运行测试版。除了在iphone6设备上,一切都正常工作 会话从后摄像头开始,每次iPhone 6用户切换到前摄像头时,应用程序就会崩溃。(需要明确的是:任何其他型号的iPhone上都没有人遇到过这个问题。)我已经着手测试了一个6,并且能够始终如一地重现错误,结果导致libc++abi.dylib:以NSException类型的未捕获异常终止 我试着在前摄像头上启动会话,但它立即崩溃 func initializeCamera() { self.captu

我最近开始在基于摄像头的应用程序上运行测试版。除了在iphone6设备上,一切都正常工作

会话从后摄像头开始,每次iPhone 6用户切换到前摄像头时,应用程序就会崩溃。(需要明确的是:任何其他型号的iPhone上都没有人遇到过这个问题。)我已经着手测试了一个6,并且能够始终如一地重现错误,结果导致
libc++abi.dylib:以NSException类型的未捕获异常终止

我试着在前摄像头上启动会话,但它立即崩溃

func initializeCamera() {
    self.captureSession.sessionPreset = .hd1920x1080

    let discovery = AVCaptureDevice.DiscoverySession.init(deviceTypes: [AVCaptureDevice.DeviceType.builtInWideAngleCamera],
                                                          mediaType: .video,
                                                          position: .unspecified) as AVCaptureDevice.DiscoverySession

    for device in discovery.devices as [AVCaptureDevice] {
        if device.hasMediaType(.video) {
            if device.position == AVCaptureDevice.Position.front {
                videoCaptureDevice = device
                do {
                    try currentDeviceInput = AVCaptureDeviceInput(device: device)
                } catch {
                    print("error: \(error.localizedDescription)")
                }
            }
        }
    }

    if videoCaptureDevice != nil {
        do {
            let videoInput = try AVCaptureDeviceInput(device: videoCaptureDevice!)
            captureSession.addInput(videoInput)

            if let audioInput = AVCaptureDevice.default(for: .audio) {
                try captureSession.addInput(AVCaptureDeviceInput(device: audioInput))
            }

            previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)

            guard let previewLayer = previewLayer else { return }

            cameraPreviewView.frame = cameraContainer.frame

            cameraPreviewView.layer.addSublayer(previewLayer)
            previewLayer.frame = cameraPreviewView.frame

            previewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill

            setVideoOrientation()

            captureSession.addOutput(movieFileOutput)

            if let movieFileOutputConnection = movieFileOutput.connection(with: .video) {
                if movieFileOutputConnection.isVideoStabilizationSupported {
                    movieFileOutputConnection.preferredVideoStabilizationMode = .cinematic
                }
            }

            captureSession.startRunning()

            sessionIsReady(true)

        } catch {
            print("error: \(error.localizedDescription)")
        }
    }

}
func setVideoOrientation() {
    if let connection = self.previewLayer?.connection {
        if connection.isVideoOrientationSupported {
            connection.videoOrientation = .portrait
            previewLayer?.frame = cameraContainer.bounds
        }
    }
}
崩溃在captureSession.addInput(videoInput)
处触发<代码>视频输入不是
nil
。相机的方向锁定为纵向


有人能提供一些见解吗?请让我知道,如果有任何额外的代码将是有益的。提前感谢。

捕获会话。addInput(视频输入)导致崩溃

if captureSession.canAddInput(videoInput) {
    captureSession.addInput(videoInput)
}
因此,您应该在之前使用
canAddInput(:)
,以避免崩溃

if captureSession.canAddInput(videoInput) {
    captureSession.addInput(videoInput)
}
在您的例子中,
captureSession.canAddInput(videoInput)
==false与iPhone 6一起使用

现在,您也在执行self.captureSession.sessionPreset=.hd1920x1080

但是,iPhone6前置摄像头硬件支持 摄像机1.2兆帕(最大1280×960像素),720p录像(每秒30帧)。似乎不适合1920*1080(“全高清”)

您可以这样做,检查您可以使用的“最大值”

func setSessionPreset(forDevice device: AVCaptureDevice) {
    let videoPresets: [AVCaptureSession.Preset] = [.hd4K3840x2160, .hd1920x1080, .hd1280x720] //etc. Put them in order to "preferred" to "last preferred"
    let preset = videoPresets.first(where: { device.supportsSessionPreset($0) }) ?? .hd1280x720
    captureSession.sessionPreset = preset
}

captureSession.addInput(videoInput)
导致崩溃

if captureSession.canAddInput(videoInput) {
    captureSession.addInput(videoInput)
}
因此,您应该在之前使用
canAddInput(:)
,以避免崩溃

if captureSession.canAddInput(videoInput) {
    captureSession.addInput(videoInput)
}
在您的例子中,
captureSession.canAddInput(videoInput)
==false与iPhone 6一起使用

现在,您也在执行self.captureSession.sessionPreset=.hd1920x1080

但是,iPhone6前置摄像头硬件支持 摄像机1.2兆帕(最大1280×960像素),720p录像(每秒30帧)。似乎不适合1920*1080(“全高清”)

您可以这样做,检查您可以使用的“最大值”

func setSessionPreset(forDevice device: AVCaptureDevice) {
    let videoPresets: [AVCaptureSession.Preset] = [.hd4K3840x2160, .hd1920x1080, .hd1280x720] //etc. Put them in order to "preferred" to "last preferred"
    let preset = videoPresets.first(where: { device.supportsSessionPreset($0) }) ?? .hd1280x720
    captureSession.sessionPreset = preset
}

这就是全部的错误信息吗?在这之前你没有更多的信息吗?不,这就是全部。Xcode将我重定向到AppDelegate,并显示线程1:signal SIGABRTTry启用异常断点。当控制台中您的消息前面没有更多消息时,这种情况很少见。但有时也会发生。
addInput的文档
此方法在调用时引发异常,而canAddInput(:)返回false。
。您能事先检查一下
let canAdd=catpureSession.canAddInput(视频输入)
?它的价值是什么?还有,iPhone6的iOS版本是什么?我很久没有玩过
AVCapure
,但这可能是因为
sessionPreset
对于iPhone6的前置摄像头来说太高了吗?或者是
内置WideAngleCamera
导致问题?这是整个错误消息吗?在这之前你没有更多的信息吗?不,这就是全部。Xcode将我重定向到AppDelegate,并显示线程1:signal SIGABRTTry启用异常断点。当控制台中您的消息前面没有更多消息时,这种情况很少见。但有时也会发生。
addInput的文档
此方法在调用时引发异常,而canAddInput(:)返回false。
。您能事先检查一下
let canAdd=catpureSession.canAddInput(视频输入)
?它的价值是什么?还有,iPhone6的iOS版本是什么?我很久没有玩过
AVCapure
,但这可能是因为
sessionPreset
对于iPhone6的前置摄像头来说太高了吗?还是
内置WideAngleCamera
导致问题?