SHARE
TWEET

Untitled

a guest Jul 24th, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. if let exportImage = finalImage {
  2.         print("Going to convert FinalImage to CMSampleBuffer")
  3.         //Need to convert FinalImage to CMSampleBuffer
  4.         var cmSampleBuffer: CMSampleBuffer {
  5.             let pixelBuffer = self.convertImageToBuffer(finalImage: exportImage)//self.convertImageToBuffer(finalImage: exportImage)//cvPixelBuffer
  6.             var newSampleBuffer: CMSampleBuffer? = nil
  7.             var timimgInfo: CMSampleTimingInfo = CMSampleTimingInfo.init(duration: .invalid, presentationTimeStamp:CMTimeMake(value: 400, timescale: 660), decodeTimeStamp:.invalid)
  8.            // var timimgInfo: CMSampleTimingInfo = CMSampleTimingInfo.invalid
  9.             var videoInfo: CMVideoFormatDescription? = nil
  10.             CMVideoFormatDescriptionCreateForImageBuffer(allocator: nil, imageBuffer: pixelBuffer!, formatDescriptionOut: &videoInfo)
  11.             CMSampleBufferCreateForImageBuffer(allocator: kCFAllocatorDefault, imageBuffer: pixelBuffer!, dataReady: true, makeDataReadyCallback: nil, refcon: nil, formatDescription: videoInfo!, sampleTiming: &timimgInfo, sampleBufferOut: &newSampleBuffer)
  12.             return newSampleBuffer!
  13.         }
  14.         print("Returned cmSampleBuffer")
  15.        // self.readAndWriteImageFromCMSampleBuffer(sample:cmSampleBuffer, bgImage: bgImage) //We can check sample buffer is correct or not
  16.         return cmSampleBuffer
  17.     }
  18.  
  19.     return nil
  20. }
  21.  
  22.  
  23. func convertImageToBuffer(finalImage: UIImage) -> CVPixelBuffer? {
  24.     let attrs = [
  25.         String(kCVPixelBufferCGImageCompatibilityKey) : kCFBooleanTrue,
  26.         String(kCVPixelBufferCGBitmapContextCompatibilityKey) : kCFBooleanTrue
  27.         ] as [String : Any]
  28.     var buffer : CVPixelBuffer?
  29.     let status = CVPixelBufferCreate(kCFAllocatorDefault, Int(finalImage.size.width), Int(finalImage.size.height), kCVPixelFormatType_32ARGB, attrs as CFDictionary, &buffer)
  30.     guard (status == kCVReturnSuccess) else {
  31.         return nil
  32.     }
  33.  
  34.     CVPixelBufferLockBaseAddress(buffer!, CVPixelBufferLockFlags(rawValue: 0))
  35.     let pixelData = CVPixelBufferGetBaseAddress(buffer!)
  36.  
  37.     let rgbColorSpace = CGColorSpaceCreateDeviceRGB()
  38.     let context = CGContext(data: pixelData, width: Int(finalImage.size.width), height: Int(finalImage.size.height), bitsPerComponent: 8, bytesPerRow: CVPixelBufferGetBytesPerRow(buffer!), space: rgbColorSpace, bitmapInfo: CGImageAlphaInfo.noneSkipFirst.rawValue)
  39.  
  40.     context?.translateBy(x: 0, y: finalImage.size.height)
  41.     context?.scaleBy(x: 1.0, y: -1.0)
  42.  
  43.     UIGraphicsPushContext(context!)
  44.     finalImage.draw(in: CGRect(x: 0, y: 0, width: finalImage.size.width, height: finalImage.size.height))
  45.     UIGraphicsPopContext()
  46.     CVPixelBufferUnlockBaseAddress(buffer!, CVPixelBufferLockFlags(rawValue: 0))
  47.  
  48.     return buffer
  49. }
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