SHARE
TWEET

Untitled

a guest Apr 24th, 2019 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extension UIImage {
  2.     func pixelBuffer() -> CVPixelBuffer? {
  3.         let frameSize = CGSize(width: self.size.width, height: self.size.height)
  4.        
  5.         var pixelBuffer:CVPixelBuffer? = nil
  6.         let status = CVPixelBufferCreate(kCFAllocatorDefault, Int(frameSize.width), Int(frameSize.height), kCVPixelFormatType_32BGRA , nil, &pixelBuffer)
  7.        
  8.         if status != kCVReturnSuccess {
  9.             return nil
  10.            
  11.         }
  12.        
  13.         CVPixelBufferLockBaseAddress(pixelBuffer!, CVPixelBufferLockFlags.init(rawValue: 0))
  14.         let data = CVPixelBufferGetBaseAddress(pixelBuffer!)
  15.         let rgbColorSpace = CGColorSpaceCreateDeviceRGB()
  16.         let bitmapInfo = CGBitmapInfo(rawValue: CGBitmapInfo.byteOrder32Little.rawValue | CGImageAlphaInfo.premultipliedFirst.rawValue)
  17.         let context = CGContext(data: data, width: Int(frameSize.width), height: Int(frameSize.height), bitsPerComponent: 8, bytesPerRow: CVPixelBufferGetBytesPerRow(pixelBuffer!), space: rgbColorSpace, bitmapInfo: bitmapInfo.rawValue)
  18.         context!.draw(self.cgImage!, in: CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height))
  19.        
  20.         CVPixelBufferUnlockBaseAddress(pixelBuffer!, CVPixelBufferLockFlags(rawValue: 0))
  21.        
  22.         return pixelBuffer
  23.     }
  24. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top