0

我关注了这个视频: https ://www.youtube.com/watch?v=7TqXrMnfJy8&t=45s 到 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
}
}
4

1 回答 1

1

我运行了你的代码,它运行得非常好——几乎!唯一的问题是我必须在应用程序的Info.plist 中添加一个 Privacy - Camera Usage Description 条目。否则应用程序崩溃。

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

So why isn't it working for you? Let's think of some possible reasons. You didn't give enough info to know for sure (seeing as the code itself works just fine), but here are some possibilities:

  • You don't have the Privacy — Camera Usage Description entry in the app's Info.plist.

  • You are testing on the Simulator. Maybe this code works only on a device.

  • There is something in your interface in front of the sublayer that you add when you say insertSublayer. To test this, try saying addSublayer instead; this will make the camera layer the frontmost layer (this is just for testing purposes, remember).

  • Maybe your code never runs at all? Perhaps we never actually go to this view controller. To test that theory, put a print statement in your viewDidLoad and see if it actually prints to the console.

  • Maybe your code runs too soon? To test that theory, move all those calls out of viewDidLoad and into something later, such as viewDidAppear. Remember, this is just for testing purposes.

Hopefully one of those will help you figure out what the problem is.

于 2018-12-09T04:56:56.123 回答