gpt4 book ai didi

ios - 拍摄 AVCaptureVideoPreviewLayer View 的快照

转载 作者:可可西里 更新时间:2023-11-01 04:33:05 26 4
gpt4 key购买 nike

我正在使用 WebRTC 在两个用户之间建立视频聊天。我想拍一张显示其中一个人的 localView View 。

这是我的类,它使用 configureLocalPreview 方法将视频流与 UIView 连接起来:

@IBOutlet var remoteView: RTCEAGLVideoView!
@IBOutlet var localView: UIView!

var captureSession: AVCaptureSession?
var videoSource: RTCAVFoundationVideoSource?
var videoTrack: RTCVideoTrack?

func configureLocalPreview() {
self.videoTrack = self.signaling.localMediaStream.self.videoTracks.first as! RTCVideoTrack?
self.videoSource = (self.videoTrack?.source as? RTCAVFoundationVideoSource)
self.captureSession = self.videoSource?.self.captureSession

self.previewLayer = AVCaptureVideoPreviewLayer.init(session: self.captureSession)
self.previewLayer.frame = self.localView.bounds
self.localView.layer.addSublayer(self.previewLayer)
self.localView.isUserInteractionEnabled = true
//self.localView.layer.position = CGPointMake(100, 100);
}

在我想访问快照的地方,我调用:

self.localView.pb_takeSnapshot()

pb_takeSnapshot 来 self 在另一篇文章中找到的 UIView 扩展。它是这样定义的:

extension UIView {
func pb_takeSnapshot() -> UIImage {
UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.main.scale)

drawHierarchy(in: self.bounds, afterScreenUpdates: true)

let image = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
}
}

当我在 Xcode 调试器中查看图像时,它看起来完全是绿色的,而我可以在 iphone 屏幕上(在该 View 内)看到的那个人却不在那里:

screenshot of the snapshot

此人不可见的原因是什么?是否无法以某种方式制作流的快照?感谢您的关注!

最佳答案

您应该使用 RTCEAGLVideoView 而不是 UIView 创建 localView。我对我的 localView 使用相同的方法,并且能够使用您帖子中提到的相同代码片段拍摄快照。

下面是启动相机并显示本地预览的示例代码:

class ViewController: UIViewController,RTCEAGLVideoViewDelegate {

var captureSession: AVCaptureSession?
var previewLayer :AVCaptureVideoPreviewLayer?
var peerConnectionFactory: RTCPeerConnectionFactory!
var videoSource:RTCAVFoundationVideoSource!
var localTrack :RTCVideoTrack!

@IBOutlet var myView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
/*myView = UIView(frame: CGRect(x: 0,
y: 0,
width: UIScreen.main.bounds.size.width,
height: UIScreen.main.bounds.size.height))*/
startCamera()
// Do any additional setup after loading the view, typically from a nib.
}

fileprivate func startCamera() {

peerConnectionFactory = RTCPeerConnectionFactory()
RTCInitializeSSL();
RTCSetupInternalTracer();
RTCSetMinDebugLogLevel(RTCLoggingSeverity.info)

videoSource = peerConnectionFactory.avFoundationVideoSource(with: nil);


localTrack = peerConnectionFactory.videoTrack(with: videoSource, trackId: "ARDAMSv0")



let localScaleX = CGFloat(1.0)
let localView : RTCEAGLVideoView = RTCEAGLVideoView(frame: self.view.bounds)
self.view.insertSubview(localView, at: 1)
localView.frame = self.view.bounds;
localView.transform = CGAffineTransform(scaleX: localScaleX, y: 1)

localTrack.add(localView)
}


override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

override func viewDidAppear(_ animated: Bool) {
//previewLayer?.frame.size = myView.frame.size
}

func videoView(_ videoView: RTCEAGLVideoView, didChangeVideoSize size: CGSize) {
print("Inside didChangeVideoSize")
}

}

关于ios - 拍摄 AVCaptureVideoPreviewLayer View 的快照,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42608915/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com