Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 使用工作表对话框时AVCaptureSession不可见_Swift_Avcapturesession - Fatal编程技术网

Swift 使用工作表对话框时AVCaptureSession不可见

Swift 使用工作表对话框时AVCaptureSession不可见,swift,avcapturesession,Swift,Avcapturesession,我在故事板中有一个自定义视图,其中有一个ViewController,可以向我显示AVCaptureSessionPreviewLayer的输出(在本例中为网络摄像头镜头)。在我的普通窗口中使用自定义视图时,一切正常,我可以看到视频输出 但是,当我想在工作表对话框中使用自定义视图时,即使网络摄像头指示视频正在运行,视频也不可见 以下是ViewController代码: import Cocoa import AVKit class RecordViewController: NSViewCon

我在故事板中有一个自定义视图,其中有一个ViewController,可以向我显示AVCaptureSessionPreviewLayer的输出(在本例中为网络摄像头镜头)。在我的普通窗口中使用自定义视图时,一切正常,我可以看到视频输出

但是,当我想在工作表对话框中使用自定义视图时,即使网络摄像头指示视频正在运行,视频也不可见

以下是ViewController代码:

import Cocoa
import AVKit

class RecordViewController: NSViewController {

    let captureSession = AVCaptureSession()
     var captureDevice : AVCaptureDevice?
     var previewLayer : AVCaptureVideoPreviewLayer?
    @IBOutlet weak var camera: NSView!

override func viewDidLoad() {
    super.viewDidLoad()

    camera.wantsLayer = true;
            camera.layer = CALayer()


            // Do any additional setup after loading the view, typically from a nib.
            captureSession.sessionPreset = AVCaptureSession.Preset.medium

            // Get all audio and video devices on this machine
            let devices = AVCaptureDevice.devices()

            // Find the FaceTime HD camera object
            for device in devices {
                print(device)

                // Camera object found and assign it to captureDevice
                if ((device as AnyObject).hasMediaType(AVMediaType.video)) {
                    print(device)
                    captureDevice = device as? AVCaptureDevice
                }
            }


            if captureDevice != nil {

                do {

                    try captureSession.addInput(AVCaptureDeviceInput(device: captureDevice!))
                    previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
                    previewLayer?.frame = (self.camera.layer?.frame)!

                    // Add previewLayer into custom view
                    self.camera.layer?.addSublayer(previewLayer!)

                    // Start camera
                    captureSession.startRunning()

                } catch {
                    print(AVCaptureSessionErrorKey.description)
                }
            }    }

}
更改行:

previewLayer?.frame=(self.camera.layer?.frame)

致:

previewLayer?.frame=self.camera.bounds

如果您想让您的相机充满您的customView,也可以使用以下行:

previewLayer?.videoGravity=AVLayerVideoGravity.resizeAspectFill

更改行:

previewLayer?.frame=(self.camera.layer?.frame)

致:

previewLayer?.frame=self.camera.bounds

如果您想让您的相机充满您的customView,也可以使用以下行:

previewLayer?.videoGravity=AVLayerVideoGravity.resizeAspectFill