AVCapturePhotoOutput - 设置不能重复使用

AVCapturePhotoOutput - settings may not be reused

我是运行ios12swift4.2.

我已经实现了一个基本的相机捕获会话,并且正在从中单击图像。一切都很好,直到我在 auto/on/off 模式之间切换闪光灯。单击第一张照片然后更改闪光灯模式后,应用程序崩溃并出现错误:

[AVCapturePhotoOutput capturePhotoWithSettings:delegate:] Settings may not be re-used'

以下是相机实现:

var captureSession: AVCaptureSession!
var videoPreviewLayer: AVCaptureVideoPreviewLayer!
var capturePhotoOutput: AVCapturePhotoOutput!
let capturePhotoSettings = AVCapturePhotoSettings()


var previewView: UIView!


override func viewDidLoad() {
    startCameraSession()
    setupCaptureOutput()
}

@objc // Tap on a button to capture
func takePhotoOnTap() {
    guard let capturePhotoOutput = self.capturePhotoOutput else { return }

    capturePhotoSettings.isAutoStillImageStabilizationEnabled = true
    capturePhotoSettings.isHighResolutionPhotoEnabled = true
    capturePhotoSettings.flashMode = .auto
    let _ = getSettings(camera: captureDevice!, flashMode: spotmiCameraOptions.flashMode)
    capturePhotoOutput.capturePhoto(with: capturePhotoSettings, delegate: self)
}


    //This is a delegate method from the button
func toggleFlash(mode: FlashMode) {

    switch mode {
    case .auto:
        capturePhotoSettings.flashMode = .auto
    case .enabled:
        capturePhotoSettings.flashMode = .on
    case .disabled:
        capturePhotoSettings.flashMode = .off
    }

}


func setupCaptureOutput() {
    capturePhotoOutput = AVCapturePhotoOutput()
    capturePhotoOutput.isHighResolutionCaptureEnabled = true
    captureSession.addOutput(capturePhotoOutput)
}

func startCameraSession() {

    let captureDevice = AVCaptureDevice.default(.builtInDualCamera, for: AVMediaType.video, position: .back)

    do {
        let input = try AVCaptureDeviceInput(device: captureDevice!)
        captureSession = AVCaptureSession()
        captureSession.addInput(input)
        videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        videoPreviewLayer.videoGravity = .resizeAspectFill
        videoPreviewLayer.frame = self.view.layer.bounds
        camcontainer.layer.addSublayer(videoPreviewLayer)
        captureSession.startRunning()
    } catch {
        print(error.localizedDescription)
    }


}

func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photoSampleBuffer: CMSampleBuffer?, previewPhoto previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) {
    guard error == nil, let sampleBuffer = photoSampleBuffer else {
        print("error capturing photo due to \(String(describing: error?.localizedDescription))")
        return
    }
    guard let imageData = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: sampleBuffer, previewPhotoSampleBuffer: previewPhotoSampleBuffer) else  { return }

    let capturedImage = UIImage(data: imageData, scale: 1.0)
    if let image = capturedImage {
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
    }
}

    func getSettings(camera: AVCaptureDevice, flashMode: FlashMode) -> AVCapturePhotoSettings {
    let settings = capturePhotoSettings

    if camera.hasFlash {
        switch flashMode {
//            case .auto: settings.flashMode = .auto
        case .enabled: settings.flashMode = .on
        case .disabled: settings.flashMode = .off
        default: settings.flashMode = .auto
        }
    }
    return settings
}

我真的不明白如何准确地重复使用 captureSettings 并每次使用不同的闪光模式更改它。我经历了几个问题,但它们主要是关于手电筒的。我在找闪光灯。

非常感谢任何帮助。

引自 AVCapturePhotoSettings 文档,请参阅最后一个重要部分:

Summary

A specification of the features and settings to use for a single photo capture request.

声明

class AVCapturePhotoSettings : NSObject Discussion

To take a photo, you create and configure a AVCapturePhotoSettings object, then pass it to the AVCapturePhotoOutput capturePhoto(with:delegate:) method. A AVCapturePhotoSettings instance can include any combination of settings, regardless of whether that combination is valid for a given capture session. When you initiate a capture by passing a photo settings object to the AVCapturePhotoOutputcapturePhoto(with:delegate:) method, the photo capture output validates your settings to ensure deterministic behavior. For example, the flashMode setting must specify a value that is present in the photo output’s supportedFlashModes array. For detailed validation rules, see each property description below.

重要

It is illegal to reuse a AVCapturePhotoSettings instance for multiple captures. Calling the capturePhoto(with:delegate:) method throws an exception (invalidArgumentException) if the settings object’s uniqueID value matches that of any previously used settings object. To reuse a specific combination of settings, use the init(from:) initializer to create a new, unique AVCapturePhotoSettings instance from an existing photo settings object.

和你一样的问题:this github issue

AVCapturePhotoSettings对象是唯一的,不能重复使用,所以每次使用此方法都需要重新设置:

func getSettings(camera: AVCaptureDevice, flashMode: CurrentFlashMode) -> AVCapturePhotoSettings {
    let settings = AVCapturePhotoSettings()

    if camera.hasFlash {
        switch flashMode {
           case .auto: settings.flashMode = .auto
           case .on: settings.flashMode = .on
           default: settings.flashMode = .off
        }
    }
    return settings
}

如您所见,不需要 lockConfiguration

CurrentFlashModeenum,创建它是为了让事情更清楚:

enum CurrentFlashMode { 关闭案例 案例 案例汽车 }

然后在拍摄照片时简单地使用它:

 @IBAction func captureButtonPressed(_ sender: UIButton) {
        let currentSettings = getSettings(camera: currentCamera, flashMode: currentFlashMode)
        photoOutput.capturePhoto(with: currentSettings, delegate: self)
    }