使用 AVCapture 在横向模式下捕获图片

Capturing a picture in Landscape mode with AVCapture

目前,我的 ios 应用程序设置为在 运行 时处于横向模式。视图中的相机预览也是横向的,但是当我拍照时,它是以纵向模式拍摄的。我想知道如何只在横向模式下捕捉图像。下面是我的相机预览和拍照功能的代码。

override func viewWillAppear(animated: Bool) {

    let devices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo)
    for device in devices {
        if device.position == AVCaptureDevicePosition.Back {

            do {
                let input = try AVCaptureDeviceInput(device: device as! AVCaptureDevice)
                if captureSession.canAddInput(input){
                    captureSession.addInput(input)
                    sessionOutput.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]

                    if captureSession.canAddOutput(sessionOutput){
                        captureSession.addOutput(sessionOutput)
                        captureSession.startRunning()
                        previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
                        previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
                        previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.LandscapeRight
                        CameraView.layer.addSublayer(previewLayer)

                        previewLayer.position = CGPoint(x: self.CameraView.frame.width / 2, y: self.CameraView.frame.height / 2)
                        previewLayer.bounds = CameraView.frame

                    }
                }

            }
            catch{
                print("ERror")
            }
        }
    }
}


@IBAction func TakePhoto(sender: UIButton) {
    tags = tags + 1
    TagsFired.text = "Tags Fired \(tags)"

    if let videoConnection = sessionOutput.connectionWithMediaType(AVMediaTypeVideo){

        sessionOutput.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: {
            buffer, error in

            let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(buffer)
            UIImageWriteToSavedPhotosAlbum(UIImage(data: imageData)!, nil, nil, nil)

            })
    }
}

能够通过 Rotating UIImage in Swift

找到解决此问题的方法

需要将 UIImage 转换为 CGImage

http://wiki.hawkguide.com/wiki/Swift:_Convert_between_CGImage,_CIImage_and_UIImage