Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if let exportImage = finalImage {
- print("Going to convert FinalImage to CMSampleBuffer")
- //Need to convert FinalImage to CMSampleBuffer
- var cmSampleBuffer: CMSampleBuffer {
- let pixelBuffer = self.convertImageToBuffer(finalImage: exportImage)//self.convertImageToBuffer(finalImage: exportImage)//cvPixelBuffer
- var newSampleBuffer: CMSampleBuffer? = nil
- var timimgInfo: CMSampleTimingInfo = CMSampleTimingInfo.init(duration: .invalid, presentationTimeStamp:CMTimeMake(value: 400, timescale: 660), decodeTimeStamp:.invalid)
- // var timimgInfo: CMSampleTimingInfo = CMSampleTimingInfo.invalid
- var videoInfo: CMVideoFormatDescription? = nil
- CMVideoFormatDescriptionCreateForImageBuffer(allocator: nil, imageBuffer: pixelBuffer!, formatDescriptionOut: &videoInfo)
- CMSampleBufferCreateForImageBuffer(allocator: kCFAllocatorDefault, imageBuffer: pixelBuffer!, dataReady: true, makeDataReadyCallback: nil, refcon: nil, formatDescription: videoInfo!, sampleTiming: &timimgInfo, sampleBufferOut: &newSampleBuffer)
- return newSampleBuffer!
- }
- print("Returned cmSampleBuffer")
- // self.readAndWriteImageFromCMSampleBuffer(sample:cmSampleBuffer, bgImage: bgImage) //We can check sample buffer is correct or not
- return cmSampleBuffer
- }
- return nil
- }
- func convertImageToBuffer(finalImage: UIImage) -> CVPixelBuffer? {
- let attrs = [
- String(kCVPixelBufferCGImageCompatibilityKey) : kCFBooleanTrue,
- String(kCVPixelBufferCGBitmapContextCompatibilityKey) : kCFBooleanTrue
- ] as [String : Any]
- var buffer : CVPixelBuffer?
- let status = CVPixelBufferCreate(kCFAllocatorDefault, Int(finalImage.size.width), Int(finalImage.size.height), kCVPixelFormatType_32ARGB, attrs as CFDictionary, &buffer)
- guard (status == kCVReturnSuccess) else {
- return nil
- }
- CVPixelBufferLockBaseAddress(buffer!, CVPixelBufferLockFlags(rawValue: 0))
- let pixelData = CVPixelBufferGetBaseAddress(buffer!)
- let rgbColorSpace = CGColorSpaceCreateDeviceRGB()
- 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)
- context?.translateBy(x: 0, y: finalImage.size.height)
- context?.scaleBy(x: 1.0, y: -1.0)
- UIGraphicsPushContext(context!)
- finalImage.draw(in: CGRect(x: 0, y: 0, width: finalImage.size.width, height: finalImage.size.height))
- UIGraphicsPopContext()
- CVPixelBufferUnlockBaseAddress(buffer!, CVPixelBufferLockFlags(rawValue: 0))
- return buffer
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement