Ios Swift-从主线程访问UIApplication.shared.statusBarOrientation
我正在尝试访问Ios Swift-从主线程访问UIApplication.shared.statusBarOrientation,ios,swift,statusbar,dispatch-queue,Ios,Swift,Statusbar,Dispatch Queue,我正在尝试访问UIApplication.shared.statusBarOrientation,但不断收到错误UIApplication.statusBarOrientation()只能从主线程使用: 我试图将它和使用它的switch语句添加到mainQueue中,但这导致它所包含的函数的返回值以nil值崩溃: DispatchQueue.main.async { let interfaceOrientation = UIApplication.shared.statusBarOr
UIApplication.shared.statusBarOrientation
,但不断收到错误UIApplication.statusBarOrientation()只能从主线程使用
:
我试图将它和使用它的switch语句添加到mainQueue中,但这导致它所包含的函数的返回值
以nil值崩溃:
DispatchQueue.main.async {
let interfaceOrientation = UIApplication.shared.statusBarOrientation
switch interfaceOrientation {
case .portrait, .portraitUpsideDown, .unknown:
videoOrientation = .portrait
case .landscapeLeft:
videoOrientation = .landscapeRight
case .landscapeRight:
videoOrientation = .landscapeLeft
@unknown default:
break
}
}
代码:
当按下camerButton以使用AVFoundation录制视频时,我访问返回值。movieFileOutput
对象访问此行的videoOrientationmovieFileOutput.connection(使用:AVMediaType.video)?.videoOrientation=videoOrientation()
:
代码:
如何解决此问题?
更新
我还试图将整个代码包装到DispatchQueue中,但出现编译错误无法将()类型的表达式返回到AVCaptureVideoOrientation类型
:
func videoOrientation() -> AVCaptureVideoOrientation {
DispatchQueue.main.async {
var videoOrientation: AVCaptureVideoOrientation!
let orientation: UIDeviceOrientation = UIDevice.current.orientation
switch orientation {
case .faceUp, .faceDown, .unknown:
let interfaceOrientation = UIApplication.shared.statusBarOrientation
switch interfaceOrientation {
case .portrait, .portraitUpsideDown, .unknown:
videoOrientation = .portrait
case .landscapeLeft:
videoOrientation = .landscapeRight
case .landscapeRight:
videoOrientation = .landscapeLeft
@unknown default:
break
}
break
case .portrait, .portraitUpsideDown:
videoOrientation = .portrait
case .landscapeLeft:
videoOrientation = .landscapeRight
case .landscapeRight:
videoOrientation = .landscapeLeft
@unknown default:
break
}
return videoOrientation
}
}
在主线程中调度另一个代码会生成值
var videoOrientation:AVCaptureVideoOrientation
为nil
当您在切换之前返回时,最好确保从主线程调用整个startRecording()
DispatchQueue.main.async {
self.startRecording()
}
由于权限检查的关闭在后台线程中运行,因此您需要在
DispatchQueue.main.async
内部调度录制调用,您的变量名和函数名相同videoOrientation
可能会出现问题…并在函数外部声明该变量以使其可访问试图在函数外部访问它,它仍然说它需要从主线程调用。嗨,我确实尝试过,但出现了另一个问题。它说“不能将()类型的表达式返回到AVCaptureVideoOrientation类型”。将所有内容包装在DispatchQueue.main.asyncI中的整个函数中,并添加了使用该函数的代码。它位于startRecording()函数中,您可以在其中调用startRecording
?它是一个按钮,用于调用它。按下cameraButton后,它将调用该函数。我在那里做了一些其他检查,以确保授予了用户的权限,如果是,则调用startRecording()函数。这就是为什么权限检查的关闭在DispatchQueue.main.async{
func videoOrientation() -> AVCaptureVideoOrientation {
DispatchQueue.main.async {
var videoOrientation: AVCaptureVideoOrientation!
let orientation: UIDeviceOrientation = UIDevice.current.orientation
switch orientation {
case .faceUp, .faceDown, .unknown:
let interfaceOrientation = UIApplication.shared.statusBarOrientation
switch interfaceOrientation {
case .portrait, .portraitUpsideDown, .unknown:
videoOrientation = .portrait
case .landscapeLeft:
videoOrientation = .landscapeRight
case .landscapeRight:
videoOrientation = .landscapeLeft
@unknown default:
break
}
break
case .portrait, .portraitUpsideDown:
videoOrientation = .portrait
case .landscapeLeft:
videoOrientation = .landscapeRight
case .landscapeRight:
videoOrientation = .landscapeLeft
@unknown default:
break
}
return videoOrientation
}
}
DispatchQueue.main.async {
self.startRecording()
}