截屏 & 录制
可能是由于SceneKit原本是设计用来做游戏渲染的框架,只提供了一个截屏的接口snapshot,拍照尚可调用,而录制并不是特别方便。
首先有一个view:
@IBOutlet var sceneView: ARSCNView!
截屏
ps:截屏中可以看到AR物体
截屏:获取UIImage对象
let re = SCNRenderer(device: sceneView.device, options: nil)
re.scene = sceneView.scene
let image = re.snapshot(atTime: CACurrentMediaTime(), with: UIScreen.main.bounds.size, antialiasingMode: .multisampling4X)
录制
要设置session的代理,代理中返回数据
ps:录制中不可以看到AR物体,只有摄像头画面
sceneView.session.delegate = self
func session(_ session: ARSession, didUpdate frame: ARFrame) {
let cvBuffer = frame.capturedImage
}
录制中如果需要同时有AR物体,还有另外一种录制方法
//ARSCNView的代理
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
renderer.delegate = self
}
//SCNSceneRenderer的代理
func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
if renderer.currentRenderPassDescriptor.colorAttachments[0].texture == nil {
return
}
let t = renderer.currentRenderPassDescriptor.colorAttachments[0].texture!
var outPixelbuffer: Unmanaged<CVPixelBuffer>?
if t.iosurface == nil {
return
}
CVPixelBufferCreateWithIOSurface(kCFAllocatorDefault, t.iosurface!, nil, &outPixelbuffer)
let buffer = outPixelbuffer?.takeRetainedValue()
}