切换到前置摄像头时 AVFoundation 摄像头崩溃(刷新摄像头)

Posted

技术标签:

【中文标题】切换到前置摄像头时 AVFoundation 摄像头崩溃(刷新摄像头)【英文标题】:AVFoundation camera crashing while switching to front camera (refreshing camera) 【发布时间】:2017-08-09 11:28:03 【问题描述】:

我是 ios 的新手,使用 swift 开发简单的应用程序,因为我需要一个自定义相机。我正在使用 AVFoundation 但该应用程序长时间显示黑屏,然后它正在加载相机。这是我的代码

 func prepareCamera() 
    captureSession.sessionPreset = AVCaptureSessionPresetPhoto
    if frontCam
    if let availableDevices = AVCaptureDeviceDiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: .front).devices 
        captureDevice = availableDevices.first
        DispatchQueue(label: "prepare").async 
        self.beginSession()
        
    
    else
        if let availableDevices = AVCaptureDeviceDiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: .back).devices 
            captureDevice = availableDevices.first
            beginSession()
        
    

@IBAction func switchCameraBtnClicked(_ sender: Any) 
    frontCam = !frontCam
    prepareCamera()

func beginSession () 

    do 
        let captureDeviceInput = try AVCaptureDeviceInput(device: captureDevice)
        if let inputs = captureSession.inputs as? [AVCaptureDeviceInput] 
            for input in inputs 
                captureSession.removeInput(input)
            
        
        if captureSession.inputs.isEmpty 
     captureSession.addInput(captureDeviceInput)
        

    catch 
        print(error.localizedDescription)
    


    if let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) 
        self.previewLayer = previewLayer
      //  self.view.layer.addSublayer(self.previewLayer)
        self.view.layer.insertSublayer(self.previewLayer, at: 0)
        self.previewLayer.frame = self.view.layer.frame

        captureSession.startRunning()

        let dataOutput = AVCaptureVideoDataOutput()
        dataOutput.videoSettings = [(kCVPixelBufferPixelFormatTypeKey as NSString):NSNumber(value:kCVPixelFormatType_32BGRA)]

        dataOutput.alwaysDiscardsLateVideoFrames = true

        if captureSession.canAddOutput(dataOutput) 
            captureSession.addOutput(dataOutput)
        

        captureSession.commitConfiguration()


        let queue = DispatchQueue(label: "com.graymatics.customcamera")
        dataOutput.setSampleBufferDelegate(self, queue: queue)



    


如果代码不正确,请纠正我。

【问题讨论】:

【参考方案1】:

终于找到解决办法了

这是我的代码:

override func viewDidDisappear(_ animated: Bool) 
         self.stopCaptureSession()
    


 func stopCaptureSession () 
        self.captureSession.stopRunning()

        if let inputs = captureSession.inputs as? [AVCaptureDeviceInput] 
            for input in inputs 
                self.captureSession.removeInput(input)
            
                
    

从当前视图控制器继续移动时需要停止会话。

【讨论】:

你有没有找到一种在录制时翻转相机的方法?

以上是关于切换到前置摄像头时 AVFoundation 摄像头崩溃(刷新摄像头)的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 swift avfoundation 设置前置摄像头变焦

AVFoundation - 相机切换一次,而且只有一次

即时切换后置/前置摄像头

如何为前置摄像头添加开关按钮?

使用AVFoundation完成照片拍摄存储相册, 开启关闭闪光灯, 切换摄像头

切换前置罕见摄像头时,HTML5 视频元素请求永远保持挂起状态(在移动设备上的 chrome 上)