priore

Convert AVCaptureStillImageOutput to UIImage

Jul 7th, 2017
159
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     // Convert AVCaptureStillImageOutput to UIImage without lose resolution
  2.     func imageFromSampleBuffer(sampleBuffer : CMSampleBuffer) -> UIImage
  3.     {
  4.         // convert UIImage without lose quality
  5.         let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
  6.         let ciImage = CIImage.init(cvPixelBuffer: pixelBuffer!)
  7.        
  8.         let context = CIContext.init()
  9.         let width = CVPixelBufferGetWidth(pixelBuffer!)
  10.         let height = CVPixelBufferGetHeight(pixelBuffer!)
  11.         let cgImage = context.createCGImage(ciImage, from: CGRect(x: 0, y: 0, width: width, height: height))
  12.            
  13.         let image = UIImage.init(cgImage: cgImage!);
  14.         return (image)
  15.     }
RAW Paste Data