Swift 我的自定义相机视图有什么问题?

Swift 我的自定义相机视图有什么问题?,swift,xcode,camera,avfoundation,Swift,Xcode,Camera,Avfoundation,我跟着这段视频:到了T。但当我打开相机视图时,我看到的只是黑屏和白色按钮。我尝试加载相机视图时没有收到错误消息。有人能帮我做错事吗 我的代码如下: import UIKit import AVFoundation class CameraViewController: UIViewController { var captureSession = AVCaptureSession() var backCamera: AVCaptureDevice? var currentCamera: AV

我跟着这段视频:到了T。但当我打开相机视图时,我看到的只是黑屏和白色按钮。我尝试加载相机视图时没有收到错误消息。有人能帮我做错事吗

我的代码如下:

import UIKit
import AVFoundation

class CameraViewController: UIViewController {

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

var photoOutput: AVCapturePhotoOutput?

var cameraPreviewLayer: AVCaptureVideoPreviewLayer?

override func viewDidLoad() {
    super.viewDidLoad()

    setupCaptureSession()
    setupDevice()
    setupInputOutput()
    setupPreviewLayer()
    startRunningCaptureSession()

}

func setupCaptureSession(){
    captureSession.sessionPreset = AVCaptureSession.Preset.photo
}
func setupDevice(){
    let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [AVCaptureDevice.DeviceType.builtInWideAngleCamera], mediaType: AVMediaType.video, position: AVCaptureDevice.Position.unspecified)
    let devices = deviceDiscoverySession.devices

    for device in devices{
        if device.position == AVCaptureDevice.Position.back {
            backCamera = device
        }
    }

    currentCamera = backCamera
}

func setupInputOutput(){

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

}

func setupPreviewLayer(){
    cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
    cameraPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
    cameraPreviewLayer?.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
    cameraPreviewLayer?.frame = self.view.frame
    self.view.layer.insertSublayer(cameraPreviewLayer!, at: 1)
}

func startRunningCaptureSession(){
    captureSession.startRunning()
}

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

我运行了你的代码,它工作得非常好-几乎!唯一的问题是,我必须在应用程序的Info.plist中添加一个隐私摄像头使用说明条目。否则,应用程序将崩溃

一旦我这么做并运行了你的代码,我就在我的设备上看到了实时摄像机视图

那为什么它对你不起作用呢?让我们考虑一些可能的原因。您没有提供足够的信息来确定(因为代码本身工作正常),但这里有一些可能性:

  • 在应用程序的Info.plist中没有隐私-摄像头使用说明条目

  • 您正在模拟器上进行测试。也许这段代码只在设备上有效

  • 当您说
    insertSublayer
    时,在子层前面的界面中添加了一些内容。要测试这一点,试着说
    addSublayer
    ;这将使相机层成为最前面的层(记住,这只是为了测试目的)

  • 也许你的代码根本就没有运行过?也许我们从未真正使用过这个视图控制器。为了测试这个理论,在
    viewDidLoad
    中放入
    print
    语句,看看它是否真的打印到控制台

  • 也许你的代码运行得太快了?要测试该理论,请将所有这些调用移出
    viewDidLoad
    ,并移到以后的某个位置,例如
    viewDidAppear
    。记住,这只是为了测试目的


希望其中一个能帮助您找出问题所在。

您正在设备上测试吗?我相信你不会在模拟器上看到任何东西。谢谢你彻底的回答,马特!这是解决问题的第四个要点。我把print语句放进去了,但在控制台中从未看到输出。原来我必须检查从目标继承!谢谢你的帮助!干杯万岁!很高兴你把它整理好了。