无法使用带闪存的AVCaptureDevice [英] Can't use AVCaptureDevice with a flash

查看:191
本文介绍了无法使用带闪存的AVCaptureDevice的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了困难时期,因为我觉得应该很简单。
我只是想在我的iOS应用程序中拍照时点亮闪光灯。我尝试过的所有功能都失败了,或只有20%的功能。

I am having difficult times, for something which I think ought to be simple. I just want to light the flash when taking a picture in my iOS app. And all I tried failed or works only 20 percent.

以下是触发闪光灯的代码:

Here is the code fired to light the flash up:

    // Here we have: captureDevice.hasFlash && captureDevice.isFlashModeSupported(.On)
    do {try captureDevice.lockForConfiguration()
        captureDevice.flashMode = .On
        captureDevice.unlockForConfiguration()
    } catch let error as NSError {
        print("captureDevice.lockForConfiguration FAILED")
        print(error.code)
    }

我尝试了几种代码,通过将try块中的2行移到最后,或者通过从不同的点调用包含此代码的函数。但在运行时的所有情况下:

I have tried several flavors of the code, by moving the 2 lines inside the try block to the end, or by calling the function containing this code from different points. But in all cases at the time of running this:

stillImageOutput.captureStillImageAsynchronouslyFromConnection(videoConnection) {
(imageDataSampleBuffer, error) -> Void in

我收到以下错误:

Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo={NSUnderlyingError=0x13f079340 {Error Domain=NSOSStatusErrorDomain Code=-16800 "(null)"}, NSLocalizedFailureReason=An unknown error occurred (-16800), NSLocalizedDescription=The operation could not be completed}

如果我然后替换该行:

captureDevice.flashMode = .On

by:

captureDevice.torchMode = .On

我得到了或多或少的工作(不幸的是更少),没有错误,但这是使用火炬,所以我得到时间问题:

I get something more or less working (unfortunately rather less), without error, but this is using the torch, so I get timing issues:

此外,我还不清楚我是否需​​要在代码中的某个位置使用此行:

Also it is not clear if I need to use this line at some point in my code:

captureSession.commitConfiguration()


推荐答案

我注意到,配置 AVCaptureDevice ,而 AVCaptureSession 包含 AVCaptureDeviceInput 引用导致此错误。我的解决方案是:在配置 AVCaptureDevice 对象之前,删除 AVCaptureSession <中的 AVCaptureDeviceInput 引用/ code>并在完成后阅读。我不会再用这种方法得到错误。
示例:

I noticed, configuring the AVCaptureDevice, while the AVCaptureSession contained an AVCaptureDeviceInput reference caused this error. My solution was: before configuring an AVCaptureDevice object, remove the AVCaptureDeviceInput reference in the AVCaptureSession and read it when completed. I would no longer get the error with this approach. Example:

self.input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:&error];


[self.session removeInput:self.input];
if ([device lockForConfiguration:NULL]) {
   device.focusMode = AVCaptureFocusModeContinuousAutoFocus;
   [device unlockForConfiguration];
}
[self.session addInput:self.input];

这篇关于无法使用带闪存的AVCaptureDevice的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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