【发布时间】:2016-03-25 22:48:48
【问题描述】:
我正在尝试处理向用户请求访问相机的权限。到目前为止,我有一个方法可以正确返回相机的当前授权状态,并且当相机权限被拒绝时,应用程序可以正常工作。
但是,如果我进入我的手机设置并将相机权限切换为“允许”,当我重新启动应用程序并尝试启动捕获会话时,我会不断遇到这种奇怪的崩溃(我的相机会在应用程序打开时立即启动仅供参考)。这是崩溃:
* 由于未捕获的异常“NSGenericException”而终止应用程序,原因:“* AVCaptureSession 无法在对 beginConfiguration / commitConfiguration 的调用之间开始运行”
这是启动会话的代码(在我提交配置之后):
dispatch_async(captureSessionQueue) {
self.captureSession.startRunning()
}
上面的代码是在我通过调用这个函数检查授权状态后调用的。只有在我检查权限已授权后,才会初始化带有捕获会话的 cameraView:
public func IsCameraAuthorized() -> AVAuthorizationStatus {
var authorizationStatus = AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo)
switch authorizationStatus {
case .NotDetermined:
// permission dialog not yet presented, request authorization
AVCaptureDevice.requestAccessForMediaType(AVMediaTypeVideo) { granted in
if granted {
authorizationStatus = .Authorized
} else {
// user denied
authorizationStatus = .Denied
}
}
break
case .Authorized:
// go
break
case .Denied, .Restricted:
// the user explicitly denied camera usage or is not allowed to access the camera devices
break
}
return authorizationStatus
}
对我做错了什么有什么想法吗?如果我在开始会话之前提交配置,为什么这不起作用?
【问题讨论】:
-
其实它的默认行为....