【问题标题】:AVCapturePhotoCaptureDelegate methods aren't being called every time不是每次都调用 AVCapturePhotoCaptureDelegate 方法
【发布时间】:2023-04-11 04:33:01
【问题描述】:

我正在构建一个自定义相机,但结果很糟糕。当我尝试捕获图像时,我有时没有收到预期的委托回调。例如,这是我的委托方法:

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

这是怎么回事?

【问题讨论】:

  • 嗯,奇怪。您是否尝试过使用单独的类作为委托并为每个捕获请求创建一个新实例(如 Apple 的 AVCam 示例中)?我可以想象由于某种原因,某些东西正在阻止处理您捕获的图像......
  • 有没有人能解决这个问题?我也得到了这种确切的行为。

标签: ios avfoundation avcapturesession


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,但这是因为我在调用 capturePhoto(with: settings, delegate: self) 后立即在捕获会话中调用了 stopRunning()

    我最终删除了stopRunning() 调用,它得到了修复。

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

    但是,考虑到您需要在某些时候致电stopRunning()(例如在viewWillDisappear

    我从 Apple 找到了这个对我很有帮助的例子 https://developer.apple.com/documentation/avfoundation/cameras_and_media_capture/avcam_building_a_camera_app

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-15
      • 2020-07-15
      • 2021-06-05
      • 2017-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多