Swift 3、iOS 10+、xCode 8.3+、Quickblox - 在保持纵横比 ratio/video 分辨率的同时切换全屏远程流

Swift 3, iOS 10+, xCode 8.3+, Quickblox - Toggle full screen remote stream while keeping aspect ratio/video res

我正在使用 webRTC 远程视频流并将远程会话附加到此:

@IBOutlet weak var remoteVideoElement: QBRTCRemoteVideoView!

我想要发生的是当用户点击上面的出口时,远程流填满整个 window。当用户再次点击它时,它会 return 到它在布局中的正常位置。在大多数情况下,它工作正常,除了我无法保持远程视频流的正确纵横比或屏幕分辨率。谁能帮我找出正确的方法来达到预期的效果?

这是我的代码:

var isExpanded = Bool()
var videoPlayerViewCenter = CGPoint()

override func viewDidLoad() {
        super.viewDidLoad()
        isExpanded = false

        let toggleFullScreen = UITapGestureRecognizer(target: self, action: #selector (self.toggleFullScreenRemoteVideo(sender:)))
        self.view.addGestureRecognizer(toggleFullScreen)
}

func toggleFullScreenRemoteVideo(sender: QBRTCRemoteVideoView) {
        if !isExpanded {
            // GO FULL SCREEN
            UIView.animate(withDuration: 0.8, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
                self.videoPlayerViewCenter = self.remoteVideoElement.center
                self.view.bringSubview(toFront: self.modalShadeBackground)
                self.view.bringSubview(toFront: self.remoteVideoElement)
                self.remoteVideoElement.frame = CGRect(x: 0, y: 0, width: self.view.frame.height, height: self.view.frame.width)
                self.remoteVideoElement.frame = AVMakeRect(aspectRatio: (self.remoteVideoElement.layer.preferredFrameSize()), insideRect: self.remoteVideoElement.frame)
                self.remoteVideoElement.contentMode = .scaleAspectFit
                self.remoteVideoElement.frame = UIScreen.main.bounds
                self.remoteVideoElement.center = self.view.center
                self.remoteVideoElement.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi / 2))
                self.remoteVideoElement.layoutSubviews()
            }, completion: nil)
        } else {
            // REVERT BACK TO ORIGINAL CONTRAINTS IN THE LAYOUT
            UIView.animate(withDuration: 0.8, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
                self.remoteVideoElement.transform = CGAffineTransform.identity
                self.remoteVideoElement.center = self.videoPlayerViewCenter
                self.view.sendSubview(toBack: self.remoteVideoElement)
                self.view.sendSubview(toBack: self.modalShadeBackground)
                self.remoteVideoElement.frame = AVMakeRect(aspectRatio: (self.remoteVideoElement.layer.preferredFrameSize()), insideRect: self.remoteVideoElement.frame)
                self.remoteVideoElement.layoutSubviews()
            }, completion: nil)
        }
        isExpanded = !isExpanded
    }

我不确定 QBRTCRemoteVideoView 但我解决了视频流的类似问题 通过获取我的视频图像的比例并将其转换为我的视频视图的矩形。 基本上是这样的:

let xScale = maxImageDrawingArea.size.width / imageSize.width    
let yScale = maxImageDrawingArea.size.height / imageSize.height
let scaleFactor = xScale < yScale ? xScale : yScale;

然后通过

获取您的视图大小
let height = maxImageDrawingArea.size.height * scaleFactor
let width = maxImageDrawingArea.size.width * scaleFactor

然后使用此高度和宽度在您的案例 remoteVideoElement 中绘制视频支架

告诉我进展如何;

祝你好运,