iOS10,相机许可 - 用于"相机应用的黑屏"

时间:2016-10-03 16:23:27

标签: ios camera ios10

当然,对于iOS 10,你现在必须这样做

enter image description here

使用手机的相机。首次启动时,用户会收到诸如

之类的问题

enter image description here

一切都很顺利。

但是我们有一个客户端应用程序是一个"相机应用程序"当你启动应用程序它只是立即启动相机,当应用程序运行时相机正在运行并显示全屏。这样做的代码是通常的方式,见下文。

问题是 - 在手机上首次启动应用程序,用户被问到问题;用户说是的。但是,在我们尝试的设备上,相机只是黑色。它不会崩溃(就像你忘记了plist项目一样)但是它会变黑并保持黑色。

如果用户退出应用并再次启动它 - 没关系,一切正常。

"相机应用程序的工作流程到底是什么?我看不到一个好的解决方案,但必须有一个用于各种相机应用程序的应用程序那里 - 当你启动应用程序时立即进入全屏相机。

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
        }

2 个答案:

答案 0 :(得分:4)

注意: 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 {    $0.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即可。在我的相机实现中,相机输入不会覆盖整个视图,还有一些导航功能。

答案 1 :(得分:3)

发生的事情是,在第一次启动时,您需要先激活相机,然后才能检查权限是什么,并显示相应的UIAlertController。您要做的是将此代码包含在if语句中以检查摄像机权限的状态(AVAuthorizationStatus)。如果在显示相机之前不允许请求许可,请确保。有关更多帮助,请参阅this question