gpt4 book ai didi

ios - AVCapturePhotoOutput - 设置不能重复使用

转载 作者:行者123 更新时间:2023-11-28 11:39:31 25 4
gpt4 key购买 nike

我正在运行 ios 12 swift 4.2。

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

[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 对象是唯一的,不能重复使用,因此每次使用此方法都需要获取新的设置:

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,创建它是为了让事情更清楚:

枚举 CurrentFlashMode { 关闭案例 案例 案例汽车

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

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

关于ios - AVCapturePhotoOutput - 设置不能重复使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54084683/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com