我从Arsessiondelegate获得CVPixelBuffer:
func session(_ session: ARSession, didUpdate frame: ARFrame) {
frame.capturedImage // CVPixelBufferRef
}
但是我的应用程序的另一部分(我无法更改)使用cmsamplebuffer。
cmsamplebuffer是cvpixelbuffer的容器。
为了创建一个cmsamplebuffer,我可以使用此功能:
func CMSampleBufferCreateReadyWithImageBuffer(_ allocator: CFAllocator?,
_ imageBuffer: CVImageBuffer,
_ formatDescription: CMVideoFormatDescription,
_ sampleTiming: UnsafePointer<CMSampleTimingInfo>,
_ sBufOut: UnsafeMutablePointer<CMSampleBuffer?>) -> OSStatus
对我来说唯一缺少的参数是sampleTiming
-如何从cvpixelbuffer中提取该参数?
采样主要包含presentationTimeStamp
,您可以通过以下代码来轻松创建它:
let scale = CMTimeScale(NSEC_PER_SEC)
let pts = CMTime(value: CMTimeValue(frame.timestamp * Double(scale)),
timescale: scale)
var timingInfo = CMSampleTimingInfo(duration: kCMTimeInvalid,
presentationTimeStamp: pts,
decodeTimeStamp: kCMTimeInvalid)