来自AVFoundation的UIImagePNG表示返回nil

时间:2016-10-29 12:45:51

标签: ios swift swift3

我有一个捕获输出委托方法:

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()它返回零,我不知道为什么。如果我使用CameraViewController所有工作完美,所以我认为问题出在AVFoundation。有什么提示吗?

1 个答案:

答案 0 :(得分:3)

问题是UIImage(ciImage:)没有将CIImage转换为普通的UIImage。您需要渲染到由CGImage支持的普通UIImage。