【发布时间】:2019-01-08 02:57:10
【问题描述】:
我正在运行 ios 12 swift 4.2。
我已经实现了一个基本的摄像头捕获会话,并且正在单击其中的图像。一切都很好,直到我在自动/开/关模式之间切换闪光灯。单击第一张照片然后更改闪光模式后,应用程序崩溃并出现错误:
[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 并每次使用不同的闪存模式进行更改。我经历了几个问题,但它们主要是关于手电筒的。我正在寻找闪光灯。
非常感谢任何帮助。
【问题讨论】:
标签: ios swift camera avfoundation avcapturesession