在实施AVCapturePhotoCaptureDelegate
的方法didFinishProcessingPhotoSampleBuffer
时,我尝试在捕获图像后填充previewView
。因此,如果用户对图像不满意,稍后会有另一个按钮触发previewView
开始显示预览以处理图像捕获。这是我能走多远:
func capture(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhotoSampleBuffer photoSampleBuffer: CMSampleBuffer?, previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) {
if let error = error {
print("Error capturing photo: \(error)")
} else {
if let sampleBuffer = photoSampleBuffer, let previewBuffer = previewPhotoSampleBuffer, let dataImage = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: sampleBuffer, previewPhotoSampleBuffer: previewBuffer) {
if let image = UIImage(data: dataImage) {
UIGraphicsBeginImageContext(previewView.frame.size)
image.draw(in: previewView.bounds)
if let imageToEmbed: UIImage = UIGraphicsGetImageFromCurrentImageContext() {
UIGraphicsEndImageContext()
self.previewView.backgroundColor = UIColor(patternImage: imageToEmbed)
} else {
UIGraphicsEndImageContext()
print("image not available")
}
}
}
}
}
如上所示,我使用UIGraphicsBeginImageContext
但无法将previewView
设置为刚捕获的图像。怎么能实现这一目标?谢谢。