How to capture AR frames as UIImages at 30FPS in ARKit.

Oscar de la Hera
AR Tips And Tricks
Published in
1 min readNov 28, 2018

For a full tutorial on how to capture video — click here.

In any case, here’s how to do it:

Step One: Add these variables to your system

var isRecording:Bool = falsevar snapshotArray:[[String:Any]] = [[String:Any]]()var lastTime:TimeInterval = 0

Step Two: Add the functionality

In your func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) call the following function — and yes, it does have to be coded this way, else your system freezes or crashes.

public func didUpdateAtTime(time: TimeInterval) {if self.isRecording {if self.lastTime == 0 || (self.lastTime + 1/31) < time {DispatchQueue.main.async { [weak self] () -> Void inguard self != nil else { return }self!.lastTime = time;let snapshot:UIImage = self!.sceneView.snapshot()let scale = CMTimeScale(NSEC_PER_SEC)self!.snapshotArray.append(["image":snapshot,"time": CMTime(value: CMTimeValue((self?.sceneView.session.currentFrame!.timestamp)! * Double(scale)), timescale: scale)]);}}

You now have a functionality that can be triggered by a button which creates an array with snapshots as UIImages and timestamps in seconds.

Step Three: Camera Button Functionality

Start Recording:

func startRecording() {self.lastTime = 0;self.isRecording = true;}

End Recording:

func stopRecording() {self.isRecording = false;print("ARRAY COUNT : \(self.snapshotArray.count) ")self.snapshotArray.removeAll();
Filemanager.default.clearTmpFiles();
}

That it for now folks.

O

--

--

Oscar de la Hera
AR Tips And Tricks

Oscar is an award-winning Spanish Inventor whose work impacts lives through brands that include Nike, MoMA and Samsung.