xcode - 在 Swift 中仅以横向模式打开相机和库

标签 xcode swift landscape

我想以横向模式打开相机和图库,但我无法做到这一点。

请有人解释一下并给我一个例子。

我使用了下面的代码,但似乎缺少一些代码(例如:导入库),我需要一个完整的示例,我如何实现,打开库和相机并保存在相机胶卷中的功能。

 override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
        if let connection =  self.previewLayer?.connection  {
            var currentDevice: UIDevice = UIDevice.currentDevice()
            var orientation: UIDeviceOrientation = currentDevice.orientation

        var previewLayerConnection : AVCaptureConnection = connection

        if (previewLayerConnection.supportsVideoOrientation)
        {
            switch (orientation)
            {
            case .Portrait:
                previewLayerConnection.videoOrientation = AVCaptureVideoOrientation.Portrait
                break
            case .LandscapeRight:
                previewLayerConnection.videoOrientation = AVCaptureVideoOrientation.LandscapeLeft
                break
            case .LandscapeLeft:
                previewLayerConnection.videoOrientation = AVCaptureVideoOrientation.LandscapeRight
                break
            case .PortraitUpsideDown:
                previewLayerConnection.videoOrientation = AVCaptureVideoOrientation.PortraitUpsideDown
                break


            default:
                previewLayerConnection.videoOrientation = AVCaptureVideoOrientation.Portrait
                break
            }
        }                
    }
}

还表明我已经实现了一些代码 我发现无需使用 AVFoundation 等其他库即可更轻松地实现。

我目前正在使用此代码来打开库和相机。

  @IBAction func openLibrary(sender: AnyObject) {        ///accion del boton Library

    imagePicker.allowsEditing = false
    imagePicker.sourceType = .PhotoLibrary

    presentViewController(imagePicker, animated:true, completion: nil)

}

@IBAction func openCamera(sender: AnyObject) {        ///accion del boton Camara



    imagePicker.allowsEditing = false
    imagePicker.sourceType = .Camera



    presentViewController(imagePicker, animated:true, completion: nil)

}

最佳答案

在您的应用程序委托(delegate)中使用此函数:

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
    if let presentedView = self.window?.rootViewController?.presentedViewController as? CameraViewController {
   //cameraView
        return Int(UIInterfaceOrientationMask.Landscape.rawValue)
        }
    } else {
        //other view
    }
}

swift 2

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
    if let presentedView = self.window?.rootViewController?.presentedViewController as? YourCameraViewController {
        //cameraView
        return UIInterfaceOrientationMask.Landscape
    } else {
//other view
// return here ur orientation for other view
}

}

关于xcode - 在 Swift 中仅以横向模式打开相机和库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36153273/

相关文章:

ios - 使用 http ://links in ios 打开你的应用程序

ios - 我可以直接在应用程序中自动登录某人吗?

ios - 根据设备方向正确缩放 UIImagePickerController 预览图像

css - 底部页脚不会在横向模式 iOS 7 中停留

ios - 验证错误 : Invalid Bundle. ... 中的包包含不允许的文件 'Frameworks'

ios - 如何在 iOS 中调用 image.acassets 中的图片?

swift - 动画文本没有出现在我的 UIView 对象中

ios - 快速转换通用数组会导致 fatal error

swift - 在Swift 3中使用Gmail URL方案时具有HTML正文

html - 横向打印 html 表格