我正在尝试制作一个使用相机的应用程序,但是使用以下代码,它只会显示白色屏幕。当应用程序打开时,应该会出现相机。
import UIKit
import AVFoundation
import Foundation
@IBOutlet var cameraView: UIView!
var captureSession : AVCaptureSession?
var stillImageOutput : AVCaptureStillImageOutput?
var previewLayer : AVCaptureVideoPreviewLayer?
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
captureSession = AVCaptureSession()
captureSession?.sessionPreset = AVCaptureSessionPreset1920x1080
let backCamera = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
do {
let input = try! AVCaptureDeviceInput (device: backCamera)
if (captureSession?.canAddInput(input) != nil) {
captureSession?.addInput(input)
stillImageOutput = AVCaptureStillImageOutput()
stillImageOutput?.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]
if (captureSession?.canAddOutput(stillImageOutput) != nil) {
captureSession?.addOutput(stillImageOutput)
previewLayer = AVCaptureVideoPreviewLayer (session: captureSession)
previewLayer?.videoGravity = AVLayerVideoGravityResizeAspect
previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.portrait
cameraView.layer.addSublayer(previewLayer!)
captureSession?.startRunning()
}
}
} catch {
}
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
previewLayer?.frame = cameraView.bounds
}
使用此代码,我所看到的只是一个白色屏幕,而不是正在使用的相机。谢谢你。
因此,实际上有人回答了这个问题,然后删除了答案。它说“您没有给予许可”。他是对的。
如何启用此功能的方法是转到您的帐户info.plist
(可以通过以下方法找到它:单击带有旁边蓝图的项目>转到Targets
并点击您的应用程序>并查看应该显示“自定义iOS目标属性”的字词。)
到达此处(info.plst)后,转到列表底部,并将鼠标悬停在最后一个(在我的情况下为“必需的设备功能”。单击显示的加号按钮并将其命名为“隐私-相机使用说明” ”。在其旁边的框中键入“app name
要使用相机进行... [xyz]”
这应该解决它。它解决了我的问题。从这篇文章中得到了我的参考。iOS 10.0运行时崩溃时的NSCameraUsageDescription吗?
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句