[英]AVCapturePhotoCaptureDelegate methods aren't being called every time
我正在構建一個自定義相機,並且我得到了不穩定的結果。 當我嘗試捕獲圖像時,有時我沒有收到預期的委托回調。 例如,這是我的委托方法:
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)
}
每次調用此方法時,我預計控制台中會出現以下 output:
capture called
will begin
will capture
did capture
finish process
finished capture
image set
但是,我只得到以下 output 似乎有 50/50 的機會:
capture called
did capture
這里發生了什么?
我遇到了同樣的問題。 我無法深入了解它,但我能夠通過將 output 從AVCapturePhotoOutput
切換到AVCaptureVideoDataOutput
來解決它。 我發現這篇文章真的很有幫助: https://medium.com/@barbulescualex/making-a-custom-camera-in-ios-ea44e3087563
我遇到了同樣的問題,但這是因為我在調用 capturePhoto 后立即在捕獲 session 上調用stopRunning()
capturePhoto(with: settings, delegate: self)
我最終刪除了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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.