swift - 我的自定义相机视图有什么问题?
问题描述
我关注了这个视频: 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 中添加一个 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 sayingaddSublayer
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 yourviewDidLoad
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 asviewDidAppear
. Remember, this is just for testing purposes.
Hopefully one of those will help you figure out what the problem is.
推荐阅读
- ios - iOS Firebase Pod - 找不到框架 FirebaseCore 错误
- python - 按字符串中的特定位置对列表进行排序
- java - 如何修复随机的“java.lang.NullPointerException at oracle.jbo.uicli.binding.JUCtrlHierBinding.bringNodeToRangeKeyPath”错误
- python - 使用 if ,else 条件和填充缺失值的 List Comprehension
- python - 如何使用python使用特定列对csv文件中的数据进行排序
- google-apps-script - 如何从 Google 表格和应用程序脚本中的撤消历史列表中删除 onEdit 触发器执行的操作
- excel - 多个ifs和and的公式错误中的Vlookup
- php - 如何foreach内部返回?
- angular - 我们正在尝试使用角度代码将上传的文件保存在本地文件夹中
- arduino - 在 arduino 上通过串行通信运行非阻塞步进器