Ios didFinishProcessingPhotoSampleBuffer未调用
我正在尝试学习如何使用AVFoundation拍摄和保存照片。目前,我已经能够使用一个按钮创建一个自定义相机视图,该按钮链接到一个拍照动作。单击按钮时,未调用委托方法 这是我的ViewController.swift:Ios didFinishProcessingPhotoSampleBuffer未调用,ios,swift,xcode,avfoundation,Ios,Swift,Xcode,Avfoundation,我正在尝试学习如何使用AVFoundation拍摄和保存照片。目前,我已经能够使用一个按钮创建一个自定义相机视图,该按钮链接到一个拍照动作。单击按钮时,未调用委托方法 这是我的ViewController.swift: class ViewController: UIViewController, AVCapturePhotoCaptureDelegate{ @IBOutlet weak var camerView: UIView! @IBOutlet weak var pho
class ViewController: UIViewController, AVCapturePhotoCaptureDelegate{
@IBOutlet weak var camerView: UIView!
@IBOutlet weak var photoButton: UIButton!
var captureSession : AVCaptureSession?
var sessionOutput : AVCapturePhotoOutput?
var previewLayer : AVCaptureVideoPreviewLayer?
var photoSettings : AVCapturePhotoSettings?
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
previewLayer?.frame = (self.camerView?.bounds)!
previewLayer?.position = CGPoint(x: (self.camerView?.frame.width)! / 2, y: (self.camerView?.frame.height)!/2)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
//Capture Session
captureSession = AVCaptureSession()
let devices = AVCaptureDeviceDiscoverySession(deviceTypes: [.builtInDualCamera], mediaType: AVMediaTypeVideo, position: .back)
for device in (devices?.devices)! {
do {
let input = try AVCaptureDeviceInput(device: device)
if (captureSession?.canAddInput(input))! {
captureSession?.addInput(input)
}
if (captureSession?.canAddOutput(sessionOutput))! {
captureSession?.addOutput(sessionOutput)
}
previewLayer = AVCaptureVideoPreviewLayer()
previewLayer?.session = captureSession
self.camerView.layer.addSublayer(previewLayer!)
self.camerView.addSubview(photoButton)
captureSession?.startRunning()
} catch {
print("error occurred")
}
}
}
@IBAction func takePhoto(_ sender: UIButton) {
photoSettings = AVCapturePhotoSettings(format: [AVVideoCodecKey : AVVideoCodecJPEG])
photoSettings?.flashMode = .on
sessionOutput?.capturePhoto(with: photoSettings!, delegate: self)
}
//AVCapturePhotoCaptureDelegate Functions
func capture(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhotoSampleBuffer photoSampleBuffer: CMSampleBuffer?, previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) {
print("PhotoSampleBuffer")
}
}
我在didFinisheProcessingPhotoSampleBuffer方法中尝试做的只是print(),这样我知道它正在被调用。我以后会想出如何保存(除非有人能告诉我一个好的资源来学习这个。)
如果您需要任何其他信息,请告诉我 由于
sessionOutput
变量为nil
,因此未调用capturePhoto
方法(以及代理回调)
要解决此问题,请在声明时实例化sessionOutput
变量:
var sessionOutput = AVCapturePhotoOutput()
另外,您需要在会话输出之后删除?
,因为它不再是可选的
sessionOutput.capturePhoto(with: photoSettings!, delegate: self)