我的自定义相机视图有什么问题?
What is wrong with my custom camera view?
我关注了这个视频: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
}
}
我 运行 你的代码,它运行得非常好——几乎!唯一的问题是我必须在应用程序的 Info.plist 中添加隐私 — 相机使用说明条目。否则应用程序崩溃。
一旦我这样做了 运行 你的代码,我就在我的设备上看到了实时摄像头视图。
那为什么它不适合你呢?让我们想想一些可能的原因。你没有提供足够的信息来确定(看到代码本身工作得很好),但这里有一些可能性:
您没有应用程序 Info.plist.
[=45= 中的隐私 - 相机使用说明条目]
您正在模拟器上进行测试。也许此代码仅适用于设备。
当您说 insertSublayer
时,您添加的子层 前面 的界面中有一些东西。要对此进行测试,请尝试说 addSublayer
;这将使相机层成为最前面的层(这只是为了测试目的,记住)。
也许您的代码根本无法运行?也许我们从来没有真正去到这个视图控制器。为了检验这个理论,在你的 viewDidLoad
中放置一个 print
语句,看看它是否真的打印到控制台。
也许您的代码运行得太快了?要验证该理论,请将所有这些调用移出 viewDidLoad
并移至稍后的内容,例如 viewDidAppear
。请记住,这仅用于测试目的。
希望其中之一能帮助您找出问题所在。
我关注了这个视频: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
}
}
我 运行 你的代码,它运行得非常好——几乎!唯一的问题是我必须在应用程序的 Info.plist 中添加隐私 — 相机使用说明条目。否则应用程序崩溃。
一旦我这样做了 运行 你的代码,我就在我的设备上看到了实时摄像头视图。
那为什么它不适合你呢?让我们想想一些可能的原因。你没有提供足够的信息来确定(看到代码本身工作得很好),但这里有一些可能性:
您没有应用程序 Info.plist.
[=45= 中的隐私 - 相机使用说明条目]您正在模拟器上进行测试。也许此代码仅适用于设备。
当您说
insertSublayer
时,您添加的子层 前面 的界面中有一些东西。要对此进行测试,请尝试说addSublayer
;这将使相机层成为最前面的层(这只是为了测试目的,记住)。也许您的代码根本无法运行?也许我们从来没有真正去到这个视图控制器。为了检验这个理论,在你的
viewDidLoad
中放置一个print
语句,看看它是否真的打印到控制台。也许您的代码运行得太快了?要验证该理论,请将所有这些调用移出
viewDidLoad
并移至稍后的内容,例如viewDidAppear
。请记住,这仅用于测试目的。
希望其中之一能帮助您找出问题所在。