【问题标题】:Custom Camera, White screen // swift 3自定义相机,白屏 // swift 3
【发布时间】:2016-12-25 18:54:28
【问题描述】:

我正在尝试制作一个使用相机的应用程序,但使用以下代码,它只显示一个白屏。当应用程序打开时,相机应该会出现。

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
}

使用此代码,我看到的只是白屏,而不是正在使用的相机。谢谢。

【问题讨论】:

    标签: swift3 avfoundation avcapturesession


    【解决方案1】:

    所以有人真的回答了这个问题,然后删除了答案。它说“你没有给予许可”。他是对的。

    如何启用此功能是通过转到您的info.plist(可以通过以下方式找到: 点击你的项目并在它旁边的蓝图>转到Targets并点击你的应用>然后查看应该显示“自定义iOS目标属性”的内容。)

    到达此处(info.plst)后,转到列表底部并将鼠标悬停在最后一个(在我的情况下是“必需的设备功能”。单击显示的加号按钮并将其命名为“隐私 -相机使用说明”。在旁边的框中输入“app name 想要将相机用于... [xyz]”

    这应该可以解决它。它解决了我的问题。从这篇文章中得到了我的参考。 NSCameraUsageDescription in iOS 10.0 runtime crash?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多