代码之家  ›  专栏  ›  技术社区  ›  Kelvin Lau

并非每次都调用AVCapturePhotoCaptureDelegate方法

  •  0
  • Kelvin Lau  · 技术社区  · 4 年前

    我正在制作一台定制相机,但结果不稳定。当我试图捕捉图像时,我 有时 未收到预期的委托回调。例如,以下是我的委托方法:

    func photoOutput(_ output: AVCapturePhotoOutput, willBeginCaptureFor resolvedSettings: AVCaptureResolvedPhotoSettings) {
        print("will begin")
    }
    
    func photoOutput(_ output: AVCapturePhotoOutput, willCapturePhotoFor resolvedSettings: AVCaptureResolvedPhotoSettings) {
        print("will capture")
    }
    
    func photoOutput(_ output: AVCapturePhotoOutput, didCapturePhotoFor resolvedSettings: AVCaptureResolvedPhotoSettings) {
        print("did capture")
    }
    
    func photoOutput(_ output: AVCapturePhotoOutput, didFinishCaptureFor resolvedSettings: AVCaptureResolvedPhotoSettings, error: Error?) {
        print("finished capture")
    }
    
    func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
        print("finish process")
    }
    

    我有一个按钮,可以触发以下方法:

    func capture() {
        print("capture called")
        let settings = AVCapturePhotoSettings()
        captureOutput.capturePhoto(with: settings, delegate: self)
    }
    

    每次调用此方法时,我都会在控制台中看到以下输出:

    capture called
    
    will begin
    will capture
    did capture
    finish process
    finished capture
    image set
    

    然而,我只得到以下输出的可能性似乎是50/50:

    capture called
    did capture
    

    这是怎么回事?

    0 回复  |  直到 4 年前
        1
  •  1
  •   Adriana Pineda    3 年前

    我也遇到了同样的问题,但那是因为我打电话来了 stopRunning() 在呼叫后立即进行捕获会话 capturePhoto(with: settings, delegate: self)

    我最终删除了 停止运行() 打了个电话,就修好了。

    func takePicture() {
            let settings = AVCapturePhotoSettings()    
    
            imageOutput?.capturePhoto(with: settings, delegate: self)
    
            videoPreviewLayer?.connection?.isEnabled = false // to freeze the image
    }
    

    但是,请考虑您需要致电 停止运行() 在某个时刻(例如 viewWillDisappear )

    我发现苹果的这个例子真的对我很有帮助 https://developer.apple.com/documentation/avfoundation/cameras_and_media_capture/avcam_building_a_camera_app

        2
  •  0
  •   neugierige    3 年前

    我也遇到了同样的问题。我无法深入了解它,但我可以通过切换输出来绕过它 AVCapturePhotoOutput AVCaptureVideoDataOutput 我发现这篇文章真的很有帮助: https://medium.com/@barbulescualex/making-a-custom-camera-in-ios-ea44e3087563