将 PhotoCaptureDelegate.swift 中的 photoOutput(didFinishProcessingPhoto) 中的图像传递到我的 ViewController

Pass image from photoOutput(didFinishProcessingPhoto) in PhotoCaptureDelegate.swift to my ViewController

在PhotoCaptureDelegate.swift中,我有这个功能:

/// - Tag: DidFinishProcessingPhoto
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
    photoProcessingHandler(false)

    print("photo output called")

    if let error = error {
        print("Error capturing photo: \(error)")
    } else {
        photoData = photo.fileDataRepresentation()    
    }
}

拍完照片后,我希望在 ViewController 的 UIView 中将 photoData 作为静态预览图像。我试图将 photoData 传递给 ViewController 中的一个变量或调用一个函数并设置它,但是当我从 PhotoCaptureDelegate.swift.

调用它时 UIElements 为 nil

在委托中调用 didFinishProcessingPhoto 函数时,如何将 photoData 用作预览图像?

如果相关,我是这样拍照的:

@IBAction func buttonTakePhotoTapped(_ sender: UIButton) {
    let videoPreviewLayerOrientation = viewPreview.videoPreviewLayer.connection?.videoOrientation

    sessionQueue.async {
        if let photoOutputConnection = self.photoOutput.connection(with: .video) {
            photoOutputConnection.videoOrientation = videoPreviewLayerOrientation!
        }

        let photoSettings = AVCapturePhotoSettings()

        if self.videoDeviceInput.device.isFlashAvailable {
            photoSettings.flashMode = .auto
        }

        photoSettings.photoQualityPrioritization = self.photoQualityPrioritizationMode

        let photoCaptureProcessor = PhotoCaptureProcessor(with: photoSettings, willCapturePhotoAnimation: {
            // Flash the screen to signal that AVCam took a photo.
            DispatchQueue.main.async {
                self.viewPreview.videoPreviewLayer.opacity = 0
                UIView.animate(withDuration: 0.25) {
                    self.viewPreview.videoPreviewLayer.opacity = 1
                }
            }
        }, livePhotoCaptureHandler: { capturing in
            self.sessionQueue.async {
            }
        }, completionHandler: { photoCaptureProcessor in
            // When the capture is complete, remove a reference to the photo capture delegate so it can be deallocated.
            self.sessionQueue.async {
                self.inProgressPhotoCaptureDelegates[photoCaptureProcessor.requestedPhotoSettings.uniqueID] = nil
            }
        }, photoProcessingHandler: { animate in
            }
        )

        // The photo output holds a weak reference to the photo capture delegate and stores it in an array to maintain a strong reference.
        self.inProgressPhotoCaptureDelegates[photoCaptureProcessor.requestedPhotoSettings.uniqueID] = photoCaptureProcessor
        self.photoOutput.capturePhoto(with: photoSettings, delegate: photoCaptureProcessor)
    }
}

您使用几个在捕获过程的不同阶段调用的回调块来初始化 PhotoCaptureProcessor。在 completionHandler 中,您应该能够访问从 photoCaptureProcessor 中捕获的照片。像这样:

let photoCaptureProcessor = PhotoCaptureProcessor(with: photoSettings, willCapturePhotoAnimation: {
    // ...
}, livePhotoCaptureHandler: { capturing in
    // ...
}, completionHandler: { photoCaptureProcessor in
    let capturedImage = UIImage(data: photoCaptureProcessor.photoData)
    // be sure to perform UI changes in main thread
    DispatchQueue.main.async {
        // assuming the image view is named like that...
        self.imageView.image = capturedImage
    }

    // ...
}, photoProcessingHandler: { animate in
    // ...
})