简体   繁体   中英

Capturing a picture in Landscape mode with AVCapture

Currently I have my ios app set up to be in landscape mode when it is run. The camera preview in the view is also in landscape, however when I take a photo it is taking them in portrait mode. I am wondering how I can capture the image in landscape mode only. below is my code for the Camera Preview and the TakePhoto functions.

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)

            })
    }
}

was able to find a fix for this via Rotating UIImage in Swift

Needed to convert UIImage to CGImage

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM