AVFoundation's UIImagePNG view returns nil

I have a delegate method for output output:

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
        }

    }

where I add a filter to my live preview. But if I try to create an NSData from an image using this method: UIImagePNGRepresentation () it returns nil, and I don't know why. If I use CameraViewController everything works fine, so I think the problem is AVFoundation. Any tips?

0
source share
1 answer

The problem is that it UIImage(ciImage:)does not convert CIImage to regular UIImage. You need to display in a normal UIImage supported by CGImage.

+2
source

Source: https://habr.com/ru/post/1671417/


All Articles