Aak*_*ave 1 camera avfoundation ios avcapturesession swift
我正在运行 ios 12 swift 4.2。
我已经实现了一个基本的相机捕获会话,并且正在点击其中的图像。一切都很好,直到我在自动/开/关模式之间切换闪光灯。单击第一张照片然后更改闪光模式后,应用程序崩溃并显示错误:
[AVCapturePhotoOutput capturePhotoWithSettings:delegate:] Settings may not be re-used'
Run Code Online (Sandbox Code Playgroud)
以下是相机实现:
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
}
Run Code Online (Sandbox Code Playgroud)
我真的不明白如何准确地重用captureSettings并每次使用不同的闪光模式更改它。我遇到了几个问题,但它们主要是关于手电筒的。我正在寻找闪光灯。
很感谢任何形式的帮助。
小智 5
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
}
Run Code Online (Sandbox Code Playgroud)
如您所见,lockConfiguration不需要。
CurrentFlashModeis enum,它的创建是为了让事情变得清晰:
enum CurrentFlashMode { case off case on case auto }
然后只需在拍摄照片时使用它:
@IBAction func captureButtonPressed(_ sender: UIButton) {
let currentSettings = getSettings(camera: currentCamera, flashMode: currentFlashMode)
photoOutput.capturePhoto(with: currentSettings, delegate: self)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2156 次 |
| 最近记录: |