我正在尝试使用AVFoundatin在我的应用中拍照。在didFinishProcessingPhoto
块中,我运行一些代码来收集图像数据并创建要在预览屏幕中显示的图像。代码如下:
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
if let error = error {
print("Error capturing photo: \(error)")
} else {
let photoData = photo.fileDataRepresentation()
if let currentData = photoData {
let dataProvider = CGDataProvider(data: currentData as CFData)
let cgImageRef = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: CGColorRenderingIntent.defaultIntent)
let image = UIImage(cgImage: cgImageRef!, scale: 1.0, orientation: self.getImageOrientation(forCamera: self.videoDeviceInput.device.position))
let containerView = PreviewPhotoContainerView()
self.view.addSubview(containerView)
containerView.previewImageView.image = image
containerView.snp.makeConstraints { (make) in
make.edges.equalTo(self.view)
}
}
}
}
但是,每次拍摄照片时,我都会得到一个错误,指出没有包装的东西为零。经过一番挖掘,我发现它是CGImageRef
。但是,我看不到哪里出了问题。