ios - 有没有办法快速将相机设置为横向模式?

标签 ios swift camera

如何将相机设置为横向模式?每次我拍照时,图像都会保存为肖像图像。当设备处于横向模式时,照片看起来不错,但如果我在相机胶卷中看到它,它仍然是纵向模式。
这是我的拍照功能:

// take a photo
@IBAction func takePhoto(sender: AnyObject) {
    self.fullScreenView.hidden = false
    self.recordButton.enabled = false
    self.takephoto.enabled = false
    self.recordButton.hidden = true
    self.takephoto.hidden = true

    session.startRunning()

    // customize the quality level or bitrate of the output photo
    session.sessionPreset = AVCaptureSessionPresetPhoto

    // add the AVCaptureVideoPreviewLayer to the view and set the view in fullscreen
    fullScreenView.frame = view.bounds
    videoPreviewLayer.frame = fullScreenView.bounds
    fullScreenView.layer.addSublayer(videoPreviewLayer)

    // add action to fullScreenView
    gestureFullScreenView = UITapGestureRecognizer(target: self, action: #selector(ViewController.takePhoto(_:)))
    self.fullScreenView.addGestureRecognizer(gestureFullScreenView)

    // add action to myView
    gestureView = UITapGestureRecognizer(target: self, action: #selector(ViewController.setFrontpage(_:)))
    self.view.addGestureRecognizer(gestureView)

    if (preview == true) {
        if let videoConnection = stillImageOutput!.connectionWithMediaType(AVMediaTypeVideo) {
            // code for photo capture goes here...

            stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: { (sampleBuffer, error) -> Void in
                // process the image data (sampleBuffer) here to get an image file we can put in our view

                if (sampleBuffer != nil) {
                    let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
                    let image = UIImage(data: imageData, scale: 1.0)

                    self.fullScreenView.hidden = true
                    self.fullScreenView.gestureRecognizers?.forEach(self.fullScreenView.removeGestureRecognizer)
                    self.session.stopRunning()

                    // save image to the library
                    UIImageWriteToSavedPhotosAlbum(image!, nil, nil, nil)

                    self.imageViewBackground = UIImageView(frame: self.view.bounds)
                    self.imageViewBackground.image = image
                    self.imageViewBackground.tag = self.key

                    self.view.addSubview(self.imageViewBackground)
                }
            })
        }
    }
    else {
        preview = true
    }
}

提前致谢!

EDIT

我的预览看起来像这样,没关系:

http://img5.fotos-hochladen.net/uploads/bildschirmfotom4s7diaehy.png

但最后看起来是这样的:

http://img5.fotos-hochladen.net/uploads/bildschirmfoto3c2rlwtevf.png

最佳答案

我用的是这个功能,可能对你有用:

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)

        let orientation = UIDevice.currentDevice().orientation

        if UIDeviceOrientationIsPortrait(orientation) || UIDeviceOrientationIsLandscape(orientation) {
            if let videoOrientation = AVCaptureVideoOrientation(rawValue: orientation.rawValue) {
                (captureView.layer as! AVCaptureVideoPreviewLayer).connection.videoOrientation = videoOrientation
            }
        }
    }

关于ios - 有没有办法快速将相机设置为横向模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38718543/

相关文章:

swift - 如何捕获 CAAnimation

objective-c - #define 宏不接受变量值

iOS - 自定义确认 View

ios - 在 viewController 关闭时调用的函数(当应用程序退出时)?

ios - 导航栏的实时模糊效果

swift - 如何在 Swift/SpriteKit 中检测与 2 个以上对象的碰撞

swift - 如何解决此弃用问题 "abs is deprecated: Please use the ` abs(_ )` free function"

android - 如何防止android反转前置摄像头的图像?

android - 任何允许访问前置和后置摄像头的 Android 设备?

java - 如何在Android中以全分辨率保存照片?