将图像从PhotoCaptureDelegate.swift中的photoOutput(didFinishProcessingPhoto)传递到我的ViewController
在PhotoCaptureDelegate.swift中,我有以下功能:将图像从PhotoCaptureDelegate.swift中的photoOutput(didFinishProcessingPhoto)传递到我的ViewController,swift,xcode,avfoundation,Swift,Xcode,Avfoundation,在PhotoCaptureDelegate.swift中,我有以下功能: /// - Tag: DidFinishProcessingPhoto func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) { photoProcessingHandler(false) print("photo output call
/// - 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()
}
}
拍摄照片后,我希望photoData在ViewController的UIView中作为静态预览图像。我试图将photoData传递给ViewController中的变量,或者调用函数并对其进行设置,但是当我从PhotoCaptureDelegate.swift调用它时,UIElements为零
在代理中调用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
// ...
})