拍摄AVCaptureVideoPreviewLayer的快照 [英] Taking a snapshot of AVCaptureVideoPreviewLayer's view

查看:123
本文介绍了拍摄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的快照的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆