【问题标题】:How to render same UIView content (a camera preview and layers) into two UIViews side by side for VR Headset in Swift如何在 Swift 中为 VR 耳机并排渲染相同的 UIView 内容(相机预览和图层)到两个 UIView
【发布时间】:2017-02-17 13:35:45
【问题描述】:

我发现了这个问题: How to show side by side two previews of camera in iOS objective-C for vr app?

但是没有答案。

而且,我正在寻找 Swift 3 中的解决方案。
我还需要在两个预览中渲染相同的额外图层。

因此,我的问题是如何并排镜像 UIView 以播放相机预览或其他传入的视频流?在这个阶段我不需要立体视觉。我只需要并排显示相同的内容。就像是: https://itunes.apple.com/ca/app/3d-fpv-stereoscopic-3d-vr/id1008155528?mt=8

【问题讨论】:

    标签: ios opengl-es


    【解决方案1】:

    我找到了答案!答案是 CAReplicatorLayer
    Multiple-Camera-Feeds(它并不快速,但可以非常轻松地在 Swift 中重构)

    使用 CAReplicatorLayer 自动复制图层。正如文档所说,它将自动创建“......指定数量的子层(源层)副本,每个副本都可能应用几何、时间和颜色转换。”

    如果除了简单的几何或颜色转换(Think Photo Booth)之外没有很多与实时预览的交互,这将非常有用。我经常看到 CAReplicatorLayer 被用作创建“反射”效果的一种方式。

    这里是一些复制 CACaptureVideoPreviewLayer 的示例代码:

    初始化 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)];
    

    初始化 CAReplicatorLayer 并设置属性

    注意:这将复制实时预览层 *四**次。*

    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);
    

    添加图层

    注意:根据我的经验,您需要将要复制的层添加到 CAReplicatorLayer 作为子层。

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

    缺点

    使用 CAReplicatorLayer 的一个缺点是它处理所有层复制的放置。因此,它将对每个实例应用任何集合转换,并且它都将包含在其自身中。例如。无法在两个单独的单元格上复制 AVCaptureVideoPreviewLayer。

    【讨论】:

    • 这是一个很好的解决方案,但是每个 4 个预览层的大小宽度和高度都可能不同,请知道如何解决这个问题的人告诉我...
    • 嘿,我刚刚快速实现了您的解决方案,并且有两个相机预览渲染。您知道如何转换视图以使预览对齐并旋转相机供稿以便水平使用吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-26
    • 1970-01-01
    • 2022-01-18
    相关资源
    最近更新 更多