拍摄AVCaptureVideoPreviewLayer的快照

编程入门 行业动态 更新时间:2024-10-21 13:20:59
本文介绍了拍摄AVCaptureVideoPreviewLayer的快照的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用WebRTC在两个用户之间建立视频聊天.我想拍摄 localView 视图的快照,其中显示了其中一个人.

I'm using WebRTC to build a video chat between two users. I want to take a snapshot of the localView view, which shows one of the persons.

这是我的类,带有 configureLocalPreview 方法,该方法将视频流与UIViews连接起来:

This is my class with the configureLocalPreview method which connects the video streams with the UIViews:

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

在我要访问快照的地方,我打电话:

At the place I want to access the snapshot, I call:

self.localView.pb_takeSnapshot()

pb_takeSnapshot 来自我在另一篇文章中找到的UIView扩展.定义如下:

pb_takeSnapshot comes from a UIView extension which I found in another post. It's defined like this:

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屏幕上(在该视图内)可以看到的那个人不存在:

When I take a look at the image in the Xcode debugger, it looks completely green and the person, which I can see on the iphone screen (inside that view), isn't there:

该人不可见的原因是什么?以某种方式只是无法对流进行快照吗?谢谢您的光临!

What could the reason that the person isn't visible? Is it somehow just not possible to make a snaphot of a stream? Thank you for taking a look!

推荐答案

您应该使用RTCEAGLVideoView而不是UIView创建localView.我为我的localView使用了相同的代码,并且能够使用与您的帖子中提到的代码段相同的代码来拍摄快照.

You should create the localView using the RTCEAGLVideoView instead of UIView. I am using the same for my localView and able to take the snapshot using the same code snippet as mentioned in your post.

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

Below is the sample code which will start your camera and show the local preview:

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") } }

更多推荐

拍摄AVCaptureVideoPreviewLayer的快照

本文发布于:2023-10-07 15:11:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1469727.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:快照   AVCaptureVideoPreviewLayer

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!