为前置摄像头设置AVCapturedevice.flashmode拍摄静止图像时会出现“拍摄静止图像时出错”

时间:2016-09-09 15:56:44

标签: ios avfoundation avcapturesession

当我为前置摄像头设置flashmode然后调用

let videoConnection = stillImageOutput!.connectionWithMediaType(AVMediaTypeVideo)
    stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: process)

我收到以下错误消息:

  捕获静止图像时出现

错误:错误Domain = AVFoundationErrorDomain Code = -11800“操作无法完成”UserInfo = {NSUnderlyingError = 0x12eeb7200 {Error Domain = NSOSStatusErrorDomain Code = -16800“(null)”},NSLocalizedFailureReason = An发生未知错误(-16800),NSLocalizedDescription =操作无法完成}

如果我没有设置相机的flashMode然后调用:

let videoConnection = stillImageOutput!.connectionWithMediaType(AVMediaTypeVideo)
    stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: process)

前置摄像头拍摄照片并不会丢失错误。 目前,这就是我设置相机的方式:

func getCameraStreamLayer() -> CALayer? {

    captureSession = AVCaptureSession()
    captureSession!.sessionPreset = AVCaptureSessionPresetPhoto
    currentCamera = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
    stillImageOutput = AVCaptureStillImageOutput()
    stillImageOutput!.outputSettings = [ AVVideoCodecKey: AVVideoCodecJPEG ]

    if let input = try? AVCaptureDeviceInput(device: currentCamera) as AVCaptureDeviceInput{
        if captureSession!.canAddInput(input) && captureSession!.canAddOutput(stillImageOutput) {
            captureSession!.addInput(input)
            captureSession!.addOutput(stillImageOutput)
        }
    }

    return AVCaptureVideoPreviewLayer(session: captureSession)
}

func toggleFlash() {
    flash = !flash
    if flash {
        for case let (device as AVCaptureDevice) in AVCaptureDevice.devices()  {
            if device.hasFlash && device.flashAvailable {
                if device.isFlashModeSupported(.On) {
                    do {
                        try device.lockForConfiguration()
                        device.flashMode = .On
                        device.unlockForConfiguration()


                    } catch {
                        print("Something went wrong")
                    }
                }
            }
        }
    }else {//turn off flash
    }
}


func photograph(process: (CMSampleBuffer!,NSError!)->()) {
    let videoConnection = stillImageOutput!.connectionWithMediaType(AVMediaTypeVideo)
    stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: process)

}
    func flipCamera() {
    guard let session = captureSession where session.running == true else {
        return
    }

    session.beginConfiguration()
    let currentCameraInput = session.inputs[0] as! AVCaptureDeviceInput
    session.removeInput(currentCameraInput)


    let newCamera = {
        let devices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo)
        for case let device as AVCaptureDevice in devices {
            if(device.position == .Front && currentCameraInput.device.position == .Back){
                return device
            }
            if(device.position == .Back && currentCameraInput.device.position == .Front){
                return device
            }
        }
        return nil
    }() as AVCaptureDevice?
    currentCamera = newCamera!
    if let newVideoInput = try? AVCaptureDeviceInput(device: newCamera) {
        captureSession?.addInput(newVideoInput)
    }

    captureSession?.commitConfiguration()
}

我不知道该怎么做。我试图创建一个新的捕获会话,然后锁定然后为相机设置flashMode。我仍然得到同样的错误。

0 个答案:

没有答案