2016-12-25 78 views
1

我正在尝试制作使用相机的应用程序,但使用下面的代码,它只显示一个白色屏幕。当应用程序打开时,相机应该出现。自定义相机,白屏//快速3

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 
} 

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

回答

0

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

如何启用这个是要你info.plist(可通过发现: 点击你的项目与蓝图旁边>去那边Targets,挖掘你的应用程序>,看着应该说些什么“自定义iOS目标属性“)。

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

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