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

标签 ios xcode 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)
}
}

最佳答案

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

extension UIImagePickerController
{
    public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return .Landscape
    }
}

这使得所有 UIImagePickerControllers 都在你的应用场景中。您还可以将其子类化并重写此方法以仅使子类可横向显示:

class LandscapePickerController: UIImagePickerController
{
    public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return .Landscape
    }
}

最后,支持你可以返回的所有方向

return [.Landscape, .Portrait]

对于 Swift 3:

    extension UIImagePickerController
    {
        override open var shouldAutorotate: Bool {
                return true
        }
        override open var supportedInterfaceOrientations : UIInterfaceOrientationMask {
                return .all
        }
}

关于ios - 在 Swift 2.0 中以横向模式使用 UIImagePickerController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33058691/

相关文章:

ios - 应用内购买订阅

ios - 创建网页并从中异步加载应用程序数据

ios - UIBarButtonItem 的空间问题

ios - 自定义更改单元格底部边框的颜色

swift - 在 Swift 中按下按钮时将对象重置为固定 (x,y) 坐标

ios - 命令因信号 : Segmentation fault: 11 (Only Sometimes) 而失败

swift - 当对象不再在相机框架中时,如何删除 ARReferenceObject Anchor?

arrays - 如何遍历多个 UILabel

ios - archiverootobject 和 unarchiverootobject 的有效文件路径

jquery - IOS4清除缓存错误?!与 "Add to Home Screen"应用程序和 localStorage