【问题标题】:Open camera and library only in landscape mode in Swift仅在 Swift 中以横向模式打开相机和库
【发布时间】:2016-03-22 11:33:09
【问题描述】:

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

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

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

 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)

}

【问题讨论】:

  • 对不起,我一次又一次地有同样的疑问......我有点绝望,因为我找不到解决方案。
  • 好的,谢谢,我会编辑我的问题。

标签: xcode swift landscape


【解决方案1】:

在您的应用程序委托中使用此功能:

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
    }
}

斯威夫特 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
}

}

【讨论】:

  • 我正在使用 lenguaje Swift,我收到错误消息:Objective-C 方法 'application:supportedInterfaceOrientationsForWindow:' 提供​​的方法 'application(:supportedInterfaceOrientationsForWindow:)' 与可选要求方法 'application (:supportedInterfaceOrientationsForWindow:)' 在协议 'UIApplicationDelegate' dropbox.com/s/dulthl1qy61prax/…
  • 只需从 intelisens 旧参数中调用它,旧版本 swift 中的旧参数可能不同,如强或弱,或者此函数可以返回不同的类型,如 swift 2.0 中的 UIInterfaceMask
  • 来自 intelisens?我不明白该怎么做。
  • 代码似乎是客观的 C,我使用的是 Swift,xcode 7.2
  • 谢谢,现在我得到了错误:dropbox.com/s/ozw1yyo4pikzotx/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多