【问题标题】:Is there a way to set a camera into landscape mode in swift?有没有办法快速将相机设置为横向模式?
【发布时间】:2016-08-02 19:49:50
【问题描述】:

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

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

提前致谢!

编辑

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

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

但最后看起来是这样的:

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

【问题讨论】:

    标签: ios swift camera


    【解决方案1】:

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

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

    【讨论】:

    • 什么是 captureView?
    • 它是一个 AVCaptureVideoPreviewLayer,它包含连接
    • 你可以使用videoConnection.videoOrientation = ...
    • 我已经编辑了帖子也许你知道我做错了什么
    【解决方案2】:

    为 videoConnection 对象设置 videoOrientation。

    例如:

    videoConnection.videoOrientation = AVCaptureVideoOrientation.LandscapeRight

    检查设备方向并将 videoOrientation 设置为适用于纵向和横向

    希望这能解决您的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-17
      • 1970-01-01
      • 1970-01-01
      • 2014-05-31
      相关资源
      最近更新 更多