在Swift 2.0中以横向模式使用UIImagePickerController

Ber*_*aya 5 xcode ios swift

我正在编写仅限LandScape的iPad应用程序,我需要从库中拍照才能发送数据库,但图像上传屏幕仅适用于纵向模式.如何将其更改为横向模式?我读过一些关于UIPickerControllerDelegate的东西不支持横向模式,但是一些应用程序(例如iMessage)已经在使用它了.

这是我的代码:

class signUpViewController: UIViewController,UIPickerViewDataSource, UIPickerViewDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate {


func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {

    print("Image Selected")

    self.dismissViewControllerAnimated(true, completion: nil)

    profileImageView.image = image

}



@IBAction func importImage(sender: AnyObject) {

    var image = UIImagePickerController()
    image.delegate = self
    image.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
    image.allowsEditing = false

    self.presentViewController(image, animated: true, completion: nil)
}
}
Run Code Online (Sandbox Code Playgroud)

Мор*_*орт 21

它绝对支持横向模式.把这个扩展放在某个地方.最好在名为UIImagePickerController + SupportedOrientations.swift的文件中

extension UIImagePickerController
{
    public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return .Landscape
    }
}
Run Code Online (Sandbox Code Playgroud)

这使得所有UIImagePickerControllers都在您的应用程序环境中.您也可以对其进行子类化并覆盖此方法,以仅使子类具有横向能力:

class LandscapePickerController: UIImagePickerController
{
    public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return .Landscape
    }
}
Run Code Online (Sandbox Code Playgroud)

最后,为了支持所有方向,您可以返回

return [.Landscape, .Portrait]
Run Code Online (Sandbox Code Playgroud)

对于Swift 3:

    extension UIImagePickerController
    {
        override open var shouldAutorotate: Bool {
                return true
        }
        override open var supportedInterfaceOrientations : UIInterfaceOrientationMask {
                return .all
        }
}
Run Code Online (Sandbox Code Playgroud)

  • UIImagePickerController 的文档表明它不应该被子类化。“UIImagePickerController 类仅支持纵向模式。此类旨在按原样使用,不支持子类化。” https://developer.apple.com/documentation/uikit/uiimagepickercontroller (2认同)