我正在尝试制作视频预览图层,但它似乎正在运行,但屏幕上没有任何内容。这是我使用的代码:
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 Clone [353:15568] [MC] systemgroup.com.apple.configurationprofiles路径的系统组容器是/ private / var / containers / Shared / SystemGroup / systemgroup .com.apple.configurationprofiles
2017-11-23 12:36:04.043014-0500 Instagram Clone [353:15568] [MC]从公共有效用户设置中读取。
3
4
5
真
我唯一的正面迹象是,当我删除并清理应用程序时,它确实要求允许使用相机。请非常感谢任何想法和帮助。
答案 0 :(得分:0)
查看代码我可以看到的一件事是你没有将会话链接到预览层。
尝试将以下内容添加到setupPerviewLayer()
cameraPreviewLayer?.session = captureSession
答案 1 :(得分:0)
我拿走了你的代码并更改了一些部分直到工作。它现在有效。这是您的代码。
import UIKit
import AVFoundation
import CoreVideo
class ViewController: UIViewController {
@IBOutlet var recordButton: UIButton!
var windowOrientation: UIInterfaceOrientation {
return view.window?.windowScene?.interfaceOrientation ?? .unknown
}
var captureSession = AVCaptureSession()
var backCamera: AVCaptureDevice?
var frontCamera: AVCaptureDevice?
var currentCamera: AVCaptureDevice?
var videoOutput = AVCaptureVideoDataOutput()
var videoDataOutputQueue: DispatchQueue!
var photoOutput: AVCapturePhotoOutput?
//var cameraPreviewLayer: AVCaptureVideoPreviewLayer?
var cameraPreviewLayer:CALayer!
//UIOutlets
@IBOutlet weak var cameraView: UIView!
@IBOutlet weak var captureImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
self.cameraSetupSession()
self.setupDevice()
self.setupInputOutput()
self.setupPreviewLayer(on: cameraView)
self.startRunningCaptureSession()
// Do any additional setup after loading the view.
}
func cameraSetupSession(){
captureSession.sessionPreset = AVCaptureSession.Preset.photo
}
func setupDevice() {
print("2")
let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [AVCaptureDevice.DeviceType.builtInUltraWideCamera], 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!)
print(captureDeviceInput)
captureSession.addInput(captureDeviceInput)
/*
let settings: [String : Any] = [
kCVPixelBufferPixelFormatTypeKey as String: NSNumber(value: kCVPixelFormatType_32BGRA),
]
videoOutput.videoSettings = settings
//videoOutput.setSampleBufferDelegate(self, queue: sessionQueue)
if captureSession.canAddOutput(videoOutput) {
captureSession.addOutput(videoOutput)
}
*/
let videoOutput = AVCaptureMovieFileOutput()
/*
videoOutput = AVCaptureVideoDataOutput()
videoDataOutputQueue = DispatchQueue(label: "VideoDataOutputQueue")
videoOutput.setSampleBufferDelegate(self, queue:self.videoDataOutputQueue)
*/
captureSession.addOutput(videoOutput)
//photoOutput?.setPreparedPhotoSettingsArray([AVCapturePhotoSettings(format:[AVVideoCodecKey:AVVideoCodecType.jpeg])], completionHandler: nil)
//captureSession.addOutput(photoOutput!)
}catch{
return
}
}
func setupPreviewLayer(on view: UIView) {
print("4")
/*
cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
cameraPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
cameraPreviewLayer?.masksToBounds = true
cameraPreviewLayer?.connection?.videoOrientation = .portrait
self.view.layer.insertSublayer(cameraPreviewLayer!, at: 0)
cameraPreviewLayer?.frame = view.frame
cameraPreviewLayer?.session = self.captureSession
*/
cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
self.view.layer.addSublayer(self.cameraPreviewLayer)
self.cameraPreviewLayer.frame = self.view.layer.frame
}
func startRunningCaptureSession() {
print("5")
self.captureSession.startRunning()
print(self.captureSession.isRunning)
}
评论我改变的部分。以便您可以看到差异。