AVFoundation的UIImagePNG表示返回nil [英] UIImagePNG representation from AVFoundation returns nil
问题描述
我有一个捕获输出委托方法:
I have a capture output delegate method:
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) {
let filter = CIFilter(name: "CIColorControls")
let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
let cameraImage = CIImage(cvPixelBuffer: pixelBuffer!)
filter?.setValue(cameraImage, forKey: kCIInputImageKey)
filter?.setValue(0.0, forKey: kCIInputSaturationKey)
let filteredImage = UIImage(ciImage: filter!.value(forKey: kCIOutputImageKey) as! CIImage!, scale: 1.0, orientation: UIImageOrientation.right)
DispatchQueue.main.async {
self.cameraView.image = filteredImage
}
}
在其中将滤镜添加到实时预览图像.但是,如果我尝试通过以下方法从图像创建NSData:UIImagePNGRepresentation(),则返回nil,我也不知道为什么.如果我使用CameraViewController的话,一切都可以正常工作,所以我认为问题出在AVFoundation.有提示吗?
where I add filter to my live preview image. But if i try to create NSData from the image by this method: UIImagePNGRepresentation() it's return nil and I have don't idea why. If I use CameraViewController all works perfect so I think that the problem is in AVFoundation. Any tips?
推荐答案
问题是UIImage(ciImage:)
不会将CIImage转换为普通的UIImage.您需要渲染成由CGImage支持的普通UIImage.
The problem is that UIImage(ciImage:)
does not convert a CIImage to a normal UIImage. You need to render into a normal UIImage backed by a CGImage.
这篇关于AVFoundation的UIImagePNG表示返回nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!