草庐IT

ios - didFinishProcessingPhotoSampleBuffer 未调用

coder 2023-09-07 原文

我正在尝试学习如何使用 AVFoundation 拍摄和保存照片。我目前已经能够使用链接到拍照操作的按钮制作自定义相机 View 。当我单击按钮时,未调用委托(delegate)方法。

这是我的 ViewController.swift:

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() 所以我知道它正在被调用。稍后我会弄清楚如何保存(除非有人能给我指出一个很好的资源来学习这个。)

如果您需要任何其他信息,请告诉我!

最佳答案

capturePhoto 方法(因此委托(delegate)回调)没有被调用,因为您的 sessionOutput 变量是 nil

要解决此问题,请在声明时实例化 sessionOutput 变量:

var sessionOutput = AVCapturePhotoOutput()

此外,您需要删除 sessionOutput 之后的 ?,因为它不再是可选的。

sessionOutput.capturePhoto(with: photoSettings!, delegate: self)

关于ios - didFinishProcessingPhotoSampleBuffer 未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43712812/

有关ios - didFinishProcessingPhotoSampleBuffer 未调用的更多相关文章

随机推荐