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

时间:2016-03-22 11:33:10

标签: xcode swift landscape

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

请有人解释并举个例子。

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

 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)

}

1 个答案:

答案 0 :(得分:0)

你的app appate中的

使用这个函数:

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
}

}