xcode macOS RTCMTLNSVideoView视频内容模式

suzh9iv8  于 2022-12-05  发布在  Mac
关注(0)|答案(1)|浏览(184)

我正在尝试在webrtc for mac中提供RTCMTLNSVideoView,以填充整个容器视图(videoContainerView是容器NSView,rtcTrack是RTCVideoTrack)

let videoView = RTCMTLNSVideoView(frame: .zero)
videoView.frame = videoContainerView.bounds
       
videoView.makeBackingLayer()
videoView.layer = CAMetalLayer()
videoView.wantsLayer = true
videoView.layer?.backgroundColor = NSColor.red.cgColor
videoView.layer?.contentsGravity = .resizeAspectFill

rtcTrack?.add(videoView)
videoContainerView.addSubview(videoView)

它的结果是视频只适合使用一个大小(aspectFit)。我似乎不能使它充满整个容器。iOS webrtc有一个属性videoContentMode。在mac上它是失踪。如何控制视频内容模式在这种情况下在mac webrtc?任何想法感谢。
谢谢

bcs8qyzn

bcs8qyzn1#

轻松修复:

if let metalView = videoView.subviews.first(where: { view in view is MTKView }) {
    metalView.layerContentsPlacement = .scaleProportionallyToFill
}

老三回答:
正如我在评论中所说的,你必须访问私有属性metalView
为此,请使用以下代码在项目中创建名为RTCMTLNSVideoView+Private.h的文件

#import <MetalKit/MetalKit.h>
#import "WebRTC/RTCMTLNSVideoView.h"

@interface RTCMTLNSVideoView ()

@property(nonatomic, strong) MTKView *metalView;

@end

然后您可以重新指定内容模式:

videoView.metalView.layerContentsPlacement = .scaleProportionallyToFill

相关问题