当然对于 iOS 10,你现在必须这样做
使用手机的相机。首次启动时,用户会收到一个问题,例如,
一切都很好。
但我们有一个客户端应用程序是“相机应用程序”:当您启动应用程序时,它会立即启动相机,当应用程序运行时,相机正在运行并全屏显示。执行此操作的代码是通常的方式,请参见下文。
问题是——第一次在手机上启动应用程序时,用户被问到这个问题;用户说是。但是,在我们尝试过的设备上,相机只是黑色的。它不会崩溃(如果您忘记了 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
}
最佳答案
注意:很可能 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
。在我的摄像头实现中,摄像头源不会覆盖整个 View ,仍然有一些导航内容。
关于iOS10,相机权限 - 黑屏 "camera app",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39836138/