我正在尝试制作视频预览层,它似乎正在工作,但是屏幕上没有任何显示。这是我使用的代码:

var captureSession = AVCaptureSession()
var backCamera: AVCaptureDevice?
var frontCamera: AVCaptureDevice?
var currentCamera: AVCaptureDevice?

var photoOutput: AVCapturePhotoOutput?

var cameraPreviewLayer: AVCaptureVideoPreviewLayer?
//UIOutlets
@IBOutlet weak var cameraView: UIView!
@IBOutlet weak var captureImageView: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view

    self.cameraSetupSession()
    self.setupDevice()
    self.setupInputOutput()
    self.setupPerviewLayer()
    self.startRunningCaptureSession()

}

func cameraSetupSession() {
   print("1")
    captureSession.sessionPreset = AVCaptureSession.Preset.photo

}

func setupDevice() {
    print("2")
    let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [AVCaptureDevice.DeviceType.builtInWideAngleCamera], mediaType: .video , position: .unspecified)

    let devices = deviceDiscoverySession.devices

    for device in devices{

        if device.position == .back{

            backCamera = device

        }else if device.position == .front{

            frontCamera = device

        }

    }
   currentCamera = backCamera
}

func setupInputOutput() {
    print("3")
    do{

        let captureDeviceInput = try AVCaptureDeviceInput(device: currentCamera!)
        captureSession.addInput(captureDeviceInput)
        photoOutput?.setPreparedPhotoSettingsArray([AVCapturePhotoSettings(format:[AVVideoCodecKey:AVVideoCodecType.jpeg])], completionHandler: nil)

    }catch{

        displayAlert(title: "Error", message: "There was an error acseesing your camera feed.", okMessage: "Okay")
    }

}

func setupPerviewLayer() {
  print("4")
    cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
    cameraPreviewLayer?.videoGravity = .resizeAspectFill
    cameraPreviewLayer?.masksToBounds = true
    cameraPreviewLayer?.connection?.videoOrientation = .portrait
    self.view.layer.insertSublayer(cameraPreviewLayer!, at: 0)


}

func startRunningCaptureSession() {
 print("5")
    self.captureSession.startRunning()
    print(self.captureSession.isRunning)
}`


在此期间,我的日志输出是:

1个

2

2017-11-23 12:36:04.042296-0500 Instagram克隆[353:15568] [MC] systemgroup.com.apple.configurationprofiles路径的系统组容器为/private/var/containers/Shared/SystemGroup/systemgroup.com。苹果配置文件

2017-11-23 12:36:04.043014-0500 Instagram克隆[353:15568] [MC]从公共有效用户设置中读取。

3

4

5

真正

我仅有的积极迹象之一是,当我删除并清洁应用程序时,它的确要求允许使用相机。拜托,我们非常感谢任何想法和帮助。

最佳答案

查看代码,我可以看到的一件事是您没有将会话链接到预览层。

尝试将以下内容添加到setupPerviewLayer()

cameraPreviewLayer?.session = captureSession

10-08 05:54