ios - Swift - 从主线程访问 UIApplication.shared.statusBarOrientation
问题描述
我正在尝试访问,UIApplication.shared.statusBarOrientation
但我不断收到错误消息UIApplication.statusBarOrientation() must be used from main thread only
:
我试图将它和使用它的 switch 语句添加到 mainQueue 但这会导致return value
从它包含的函数到以 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
}
}
代码:
func videoOrientation() -> AVCaptureVideoOrientation {
var videoOrientation: AVCaptureVideoOrientation!
let orientation: UIDeviceOrientation = UIDevice.current.orientation
switch orientation {
case .faceUp, .faceDown, .unknown:
// ** I tried to add this alone to the mainQueue but the switch statement couldn't access it so then I tried to add the entire switch but that lead to the return value crashing with a nil
let interfaceOrientation = UIApplication.shared.statusBarOrientation
switch interfaceOrientation {
case .portrait, .portraitUpsideDown, .unknown:
videoOrientation = .portrait
case .landscapeLeft:
videoOrientation = .landscapeRight
case .landscapeRight:
videoOrientation = .landscapeLeft
@unknown default:
break
}
case .portrait, .portraitUpsideDown:
videoOrientation = .portrait
case .landscapeLeft:
videoOrientation = .landscapeRight
case .landscapeRight:
videoOrientation = .landscapeLeft
@unknown default:
break
}
return videoOrientation // ** crashes here **
}
当按下camerButton使用AVFoundation录制视频时,我访问了返回值。该movieFileOutput
对象访问该行上的 videoOrientation movieFileOutput.connection(with: AVMediaType.video)?.videoOrientation = videoOrientation()
:
代码:
let captureSession = AVCaptureSession()
var movieFileOutput = AVCaptureMovieFileOutput()
var videoFileUrlFromCamera: URL?
func startRecording() {
if !captureSession.outputs.contains(movieFileOutput) {
return
}
// Stop recording
if movieFileOutput.isRecording {
stopMovieRecordigShowControls()
} else {
if !captureSession.outputs.contains(movieFileOutput) {
return
}
// Start recording
movieFileOutput.connection(with: AVMediaType.video)?.videoOrientation = videoOrientation()
movieFileOutput.maxRecordedDuration = maxRecordDuration()
videoFileUrlFromCamera = URL(fileURLWithPath: videoFileLocation())
if let videoFileUrlFromCamera = videoFileUrlFromCamera {
movieFileOutput.startRecording(to: videoFileUrlFromCamera, recordingDelegate: self)
}
}
}
我怎样才能解决这个问题?
更新
我还尝试将整个代码包装在 DispatchQueue 中,但出现以下编译错误Cannot return expression of type () to return type 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
switch 之前触发,最好确保你startRecording()
从主线程调用整个
DispatchQueue.main.async {
self.startRecording()
}
由于权限检查的关闭在后台线程中运行,因此您需要在内部调度录制的调用DispatchQueue.main.async
推荐阅读
- c# - 如何在日期时间选择器中找到选择位置?
- android - React Native Linking:从 Firebase Dynamic Link 读取 url 参数
- maven - 在 Maven 多模块项目中,如何从另一个模块获取一个模块的 buildVersion?
- c# - 如何在 Asp.Net MVC 5.x 中使用条件序列化?
- javafx - 程序运行时,如何使 MediaView 具有相同的布局边界的高度和宽度?
- ios - UIApplication.OpenURLOptionsKey.sourceApplication 在 iOS 13 上为零?
- c++ - 我可以在单读/单写队列中用 volatile 替换原子吗?
- javascript - 我收到“参考错误:未定义按钮”错误
- java - 无法使用自定义反序列化器反序列化 JSON 对象
- swift - 功能只是快速读取最高值