1

I found the question: How to show side by side two previews of camera in iOS objective-C for vr app?

But there is no answer.

And, I am looking for the solution in Swift 3.
I also need render same extra layers in both preview.

Thus, my question is how to mirror UIView side by side to play camera preview or other incoming video stream? I don't need stereoscopic vision in this stage. I just need show same content side by side. Something like: https://itunes.apple.com/ca/app/3d-fpv-stereoscopic-3d-vr/id1008155528?mt=8

Community
  • 1
  • 1
Yi Jiang
  • 3,938
  • 6
  • 30
  • 62

1 Answers1

4

I found the answer! The answer is CAReplicatorLayer
Multiple-Camera-Feeds (It is not swift, but can be re-factorised in Swift very very easily)

use a CAReplicatorLayer to duplicate the layer automatically. As the docs say, it will automatically create "...a specified number of copies of its sublayers (the source layer), each copy potentially having geometric, temporal and color transformations applied to it."

This is super useful if there isn't a lot of interaction with the live previews besides simple geometric or color transformations (Think Photo Booth). I have most often seen the CAReplicatorLayer used as a way to create the 'reflection' effect.

Here is some sample code to replicate a CACaptureVideoPreviewLayer:

Init AVCaptureVideoPreviewLayer

AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
[previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[previewLayer setFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, self.view.bounds.size.height / 4)];

Init CAReplicatorLayer and set properties

Note: This will replicate the live preview layer *four** times.*

NSUInteger replicatorInstances = 4;

CAReplicatorLayer *replicatorLayer = [CAReplicatorLayer layer];
replicatorLayer.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height / replicatorInstances);
replicatorLayer.instanceCount = instances;
replicatorLayer.instanceTransform = CATransform3DMakeTranslation(0.0, self.view.bounds.size.height / replicatorInstances, 0.0);

Add Layers

Note: From my experience you need to add the layer you want to replicate to the CAReplicatorLayer as a sublayer.

[replicatorLayer addSublayer:previewLayer];
[self.view.layer addSublayer:replicatorLayer];

Downsides

A downside to using CAReplicatorLayer is that it handles all placement of the layer replications. So it will apply any set transformations to each instance and and it will all be contained within itself. E.g. There would be no way to have a replication of a AVCaptureVideoPreviewLayer on two separate cells.

Yi Jiang
  • 3,938
  • 6
  • 30
  • 62
  • it good solution but it possible each 4 preview layer different size width and height, please any one know how to solve this one problem let me know ... – Ramani Hitesh Feb 05 '19 at 07:40
  • Hey I just implemented your solution in swift and have two camera previews rendering. Do you know how to transform the view to get the previews to align in a row and rotate the camera feed so it can be used horizontally? – iThompkins Oct 20 '19 at 02:27