使用AVFoundation捕获照片时,在预览UIView中嵌入图像

时间:2017-07-09 01:12:52

标签: ios swift avfoundation

在实施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设置为刚捕获的图像。怎么能实现这一目标?谢谢。

0 个答案:

没有答案