gpt4 book ai didi

ios - 仅在 iPhone X (iOS 12) 上以横屏模式使用 UIImagePickerController 时应用程序崩溃

转载 作者:行者123 更新时间:2023-11-28 10:36:41 25 4
gpt4 key购买 nike

在 Xcode 10.1 中创建的项目仅具有左右设备方向景观。

在 Plist 中添加了以下键

隐私 - 相机使用说明:“用于拍照”

   import UIKit

class UploadViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate

{
//MARK:- ViewDidLoad
override func viewDidLoad() {
super.viewDidLoad()


}

@IBAction func cameraButtonAction(_ sender: UIButton)
{

if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera){

let imagePicker = UIImagePickerController()
imagePicker.sourceType = .camera
imagePicker.modalPresentationStyle = .overCurrentContext
imagePicker.delegate = self
self.present(imagePicker, animated: true, completion: nil)

}else{

let alert = UIAlertController(title: "Warning", message: "You don't have camera", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)

}
}

}

以上代码在以下设备中运行良好

iPhone 8 (iOS 12)

iPhone 7 (iOS 11.4.1)

iPad 迷你版(iOS 11)

但是当我在装有 iOS 12.1 的 iPhoneX 中运行它时,出现以下错误。

Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'Supported orientations has no common orientation with the application, and [CAMViewfinderViewController shouldAutorotate] is returning YES'
*** First throw call stack:

libc++abi.dylib: terminating with uncaught exception of type NSException

我对这个问题进行了研究,但我还没有找到解决方案。以下是我为相关问题找到的几个链接,但没有一个适用于 iPhoneX 的答案。

Using UIImagePickerController in landscape orientation

UIImagePickerController InterfaceOrientation Crash

https://www.reddit.com/r/iOSProgramming/comments/3xpwot/discussion_lets_talk_about_landscape_cameras/

最佳答案

我也一直面临这个问题,在做了这么多 RND 之后找到了解决方案:

第 1 步:制作自定义 UIImage 选择器 Controller 并使用它。相同的代码如下

class MyImagePickerViewController: UIImagePickerController {

override public func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}

override public var shouldAutorotate: Bool {
return false
}

override public var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.landscape // Since your app is in only landscape mode
}

}

第 2 步:制作一个全局标志 isOpenGallery 并在打开图库之前将其设置为真并在关闭 MyImagepickercontroller 之前将其设置为假 并在您的 App Delegate 中执行以下代码:

  func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return isOpenGallery ? UIInterfaceOrientationMask.portrait : UIInterfaceOrientationMask.landscape
}

关于ios - 仅在 iPhone X (iOS 12) 上以横屏模式使用 UIImagePickerController 时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53372686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com