仅在iPhone 7之前的设备上使用前置摄像头时崩溃 [英] Crash when using front camera ONLY on pre-iPhone 7 devices

查看:510
本文介绍了仅在iPhone 7之前的设备上使用前置摄像头时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始在基于相机的应用程序上运行Beta.除了在iPhone 6设备上,一切都按预期进行.

I've recently started running beta on my camera-based app. Everything is working as expected except on iPhone 6 devices.

会话在后置摄像头上开始,并且每次iPhone 6用户切换到前摄像头时,应用都会崩溃. (而且要非常清楚:任何其他iPhone机型上都没有人遇到此问题.)我已经把手放在6上进行测试,并且可以始终如一地重现该错误,结果为libc++abi.dylib: terminating with uncaught exception of type NSException.

The session starts on the back camera, and each time an iPhone 6 user switches to the front camera the app crashes. (And just to be really clear: no one on any other iPhone model is experiencing the issue.) I've gotten my hands on a 6 to test and can consistently reproduce the error, resulting in libc++abi.dylib: terminating with uncaught exception of type NSException.

我尝试在前置摄像头上启动会话,但该会话立即崩溃.

I've tried starting the session on the front camera and it crashes immediately.

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)处触发. videoInput不是nil.相机的方向锁定为纵向.

The crash is triggered at captureSession.addInput(videoInput). videoInput is not nil. The camera's orientation is locked to portrait.

谁能提供任何见解?请让我知道是否有任何其他代码会有所帮助.预先感谢.

Can anyone offer any insight? Please let me know if any additional code would be helpful. Thanks in advance.

推荐答案

captureSession.addInput(videoInput)导致崩溃.

因此,应先使用canAddInput(_:)以避免崩溃.

So you should use canAddInput(_:) before to avoid the crash.

if captureSession.canAddInput(videoInput) {
    captureSession.addInput(videoInput)
}

在您的情况下,该iPhone 6 captureSession.canAddInput(videoInput) == false.

And in your case, captureSession.canAddInput(videoInput) == false with that iPhone 6.

现在,您也在做self.captureSession.sessionPreset = .hd1920x1080

但是根据WikiPedia ,iPhone 6前置摄像头硬件支持 相机1.2 MP(最大1280×960像素),720p视频录制(30 fps).似乎不适合1920 * 1080(全高清").

But according to WikiPedia, the iPhone 6 Front Camera hardware supports camera 1.2 MP (1280×960 px max.), 720p video recording (30 fps). Doesn't seem to fit the 1920*1080 ("Full HD").

您可以执行此操作检查最大" AVCaptureSession.Preset 您可以使用.

You could do this check what the "max" AVCaptureSession.Preset you can use.

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
}

这篇关于仅在iPhone 7之前的设备上使用前置摄像头时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆