iOS10,相机权限 - "camera app" 的黑屏
iOS10, camera permission - black screen for "camera app"
当然 iOS 10,你现在必须这样做
使用 phone 的相机。首次启动时,用户会收到一个问题,例如
一切顺利。
但我们有一个 "camera app" 的客户端应用程序:当您启动该应用程序时 它会立即启动相机 ,当该应用程序 运行 相机 运行 并且全屏显示。这样做的代码是通常的方式,见下文。
问题是 - 在 phone 上首次启动应用程序时,系统会向用户询问该问题;用户说是。但是,在我们尝试过的设备上,相机只是黑色的。它不会崩溃(如果您忘记了 plist 项目就会崩溃)但它会变黑并保持黑色。
如果用户退出应用程序并再次启动它 - 没问题,一切正常。
"camera app" 的工作流程到底是什么? 我看不出好的解决方案,但肯定有一个适用于各种相机应用程序的解决方案- 当您启动应用程序时,它会立即进入全屏摄像头。
class CameraPlane:UIViewController
{
...
func cameraBegin()
{
captureSession = AVCaptureSession()
captureSession!.sessionPreset = AVCaptureSessionPresetPhoto
let backCamera = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
var error: NSError?
var input: AVCaptureDeviceInput!
do {
input = try AVCaptureDeviceInput(device: backCamera)
} catch let error1 as NSError
{
error = error1
input = nil
}
if ( error != nil )
{
print("probably on simulator? no camera?")
return;
}
if ( captureSession!.canAddInput(input) == false )
{
print("capture session problem?")
return;
}
captureSession!.addInput(input)
stillImageOutput = AVCaptureStillImageOutput()
stillImageOutput!.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]
if ( captureSession!.canAddOutput(stillImageOutput) == false )
{
print("capture session with stillImageOutput problem?")
return;
}
captureSession!.addOutput(stillImageOutput)
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer!.videoGravity = AVLayerVideoGravityResizeAspectFill
// or, AVLayerVideoGravityResizeAspect
fixConnectionOrientation()
view.layer.addSublayer(previewLayer!)
captureSession!.startRunning()
previewLayer!.frame = view.bounds
}
发生的事情是,在第一次启动时,您先激活相机,然后它才能检查权限是什么并显示适当的 UIAlertController
。您要做的是将此代码包含在 if 语句中以检查相机权限的状态 (AVAuthorizationStatus
)。确保如果不允许在显示相机之前请求许可。如需更多帮助,请参阅 this question。
注意:很可能 OP 的代码在新的 ISO10 权限字符串方面实际上工作正常,并且 OP 有另一个导致黑屏的问题。
根据您发布的代码,我无法判断您为什么会遇到这种行为。我只能给你适合我的代码。
此代码也在 iOS 9 上运行。请注意,我在 viewDidAppear
中加载相机以确保设置了所有约束。
import AVFoundation
class ViewController : UIViewController {
//the view where the camera feed is shown
@IBOutlet weak var cameraView: UIView!
var captureSession: AVCaptureSession = {
let session = AVCaptureSession()
session.sessionPreset = AVCaptureSessionPresetPhoto
return session
}()
var sessionOutput = AVCaptureStillImageOutput()
var previewLayer = AVCaptureVideoPreviewLayer()
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let devices = AVCaptureDevice.devices(withMediaType: AVMediaTypeVideo) as? [AVCaptureDevice]
guard let backCamera = (devices?.first { [=10=].position == .back }) else {
print("The back camera is currently not available")
return
}
do {
let input = try AVCaptureDeviceInput(device: backCamera)
if captureSession.canAddInput(input){
captureSession.addInput(input)
sessionOutput.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]
if captureSession.canAddOutput(sessionOutput) {
captureSession.addOutput(sessionOutput)
captureSession.startRunning()
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
previewLayer.connection.videoOrientation = .portrait
cameraView.layer.addSublayer(previewLayer)
previewLayer.position = CGPoint(x: cameraView.frame.width / 2, y: cameraView.frame.height / 2)
previewLayer.bounds = cameraView.frame
}
}
} catch {
print("Could not create a AVCaptureSession")
}
}
}
如果你想让相机显示全屏,你可以简单地使用 view
而不是 cameraView
。在我的摄像头实现中,摄像头馈送不覆盖整个视图,仍然有一些导航内容。
当然 iOS 10,你现在必须这样做
使用 phone 的相机。首次启动时,用户会收到一个问题,例如
一切顺利。
但我们有一个 "camera app" 的客户端应用程序:当您启动该应用程序时 它会立即启动相机 ,当该应用程序 运行 相机 运行 并且全屏显示。这样做的代码是通常的方式,见下文。
问题是 - 在 phone 上首次启动应用程序时,系统会向用户询问该问题;用户说是。但是,在我们尝试过的设备上,相机只是黑色的。它不会崩溃(如果您忘记了 plist 项目就会崩溃)但它会变黑并保持黑色。
如果用户退出应用程序并再次启动它 - 没问题,一切正常。
"camera app" 的工作流程到底是什么? 我看不出好的解决方案,但肯定有一个适用于各种相机应用程序的解决方案- 当您启动应用程序时,它会立即进入全屏摄像头。
class CameraPlane:UIViewController
{
...
func cameraBegin()
{
captureSession = AVCaptureSession()
captureSession!.sessionPreset = AVCaptureSessionPresetPhoto
let backCamera = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
var error: NSError?
var input: AVCaptureDeviceInput!
do {
input = try AVCaptureDeviceInput(device: backCamera)
} catch let error1 as NSError
{
error = error1
input = nil
}
if ( error != nil )
{
print("probably on simulator? no camera?")
return;
}
if ( captureSession!.canAddInput(input) == false )
{
print("capture session problem?")
return;
}
captureSession!.addInput(input)
stillImageOutput = AVCaptureStillImageOutput()
stillImageOutput!.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]
if ( captureSession!.canAddOutput(stillImageOutput) == false )
{
print("capture session with stillImageOutput problem?")
return;
}
captureSession!.addOutput(stillImageOutput)
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer!.videoGravity = AVLayerVideoGravityResizeAspectFill
// or, AVLayerVideoGravityResizeAspect
fixConnectionOrientation()
view.layer.addSublayer(previewLayer!)
captureSession!.startRunning()
previewLayer!.frame = view.bounds
}
发生的事情是,在第一次启动时,您先激活相机,然后它才能检查权限是什么并显示适当的 UIAlertController
。您要做的是将此代码包含在 if 语句中以检查相机权限的状态 (AVAuthorizationStatus
)。确保如果不允许在显示相机之前请求许可。如需更多帮助,请参阅 this question。
注意:很可能 OP 的代码在新的 ISO10 权限字符串方面实际上工作正常,并且 OP 有另一个导致黑屏的问题。
根据您发布的代码,我无法判断您为什么会遇到这种行为。我只能给你适合我的代码。
此代码也在 iOS 9 上运行。请注意,我在 viewDidAppear
中加载相机以确保设置了所有约束。
import AVFoundation
class ViewController : UIViewController {
//the view where the camera feed is shown
@IBOutlet weak var cameraView: UIView!
var captureSession: AVCaptureSession = {
let session = AVCaptureSession()
session.sessionPreset = AVCaptureSessionPresetPhoto
return session
}()
var sessionOutput = AVCaptureStillImageOutput()
var previewLayer = AVCaptureVideoPreviewLayer()
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let devices = AVCaptureDevice.devices(withMediaType: AVMediaTypeVideo) as? [AVCaptureDevice]
guard let backCamera = (devices?.first { [=10=].position == .back }) else {
print("The back camera is currently not available")
return
}
do {
let input = try AVCaptureDeviceInput(device: backCamera)
if captureSession.canAddInput(input){
captureSession.addInput(input)
sessionOutput.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]
if captureSession.canAddOutput(sessionOutput) {
captureSession.addOutput(sessionOutput)
captureSession.startRunning()
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
previewLayer.connection.videoOrientation = .portrait
cameraView.layer.addSublayer(previewLayer)
previewLayer.position = CGPoint(x: cameraView.frame.width / 2, y: cameraView.frame.height / 2)
previewLayer.bounds = cameraView.frame
}
}
} catch {
print("Could not create a AVCaptureSession")
}
}
}
如果你想让相机显示全屏,你可以简单地使用 view
而不是 cameraView
。在我的摄像头实现中,摄像头馈送不覆盖整个视图,仍然有一些导航内容。