I saw other answers to this question, but it does not seem to work. I am collecting an image from AVMediaTypeVideo, and its orientation is always UIImageOrientation 3 (I do not know what this really means).
I tried to fix it using the status bar orientation, but this does not seem to have any effect. I do not need to save these images in the camera frame, and it is just there to check. I'm not sure if this also affects orientation.
import UIKit
import AVFoundation
class ViewController: UIViewController {
let captureSession = AVCaptureSession()
let stillImageOutput = AVCaptureStillImageOutput()
var avCaptureVideoPreview : AVCaptureVideoPreviewLayer?
var error: NSError?
override func viewDidLoad() {
super.viewDidLoad()
let devices = AVCaptureDevice.devices().filter{ $0.hasMediaType(AVMediaTypeVideo) }
if let captureDevice = devices.first as? AVCaptureDevice {
captureSession.addInput(AVCaptureDeviceInput(device: captureDevice, error: &error))
captureSession.sessionPreset = AVCaptureSessionPresetMedium
captureSession.startRunning()
stillImageOutput.outputSettings = [AVVideoCodecKey:AVVideoCodecJPEG]
if captureSession.canAddOutput(stillImageOutput) {
captureSession.addOutput(stillImageOutput)
}
avCaptureVideoPreview = AVCaptureVideoPreviewLayer(session: captureSession)
if let previewLayer = avCaptureVideoPreview {
previewLayer.bounds = CGRectMake(0.0, 0.0, view.bounds.size.width, view.bounds.size.height)
previewLayer.position = CGPointMake(view.bounds.midX, view.bounds.midY)
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
let cameraPreview = UIView(frame: CGRectMake(0.0, 0.0, view.bounds.size.width, view.bounds.size.height))
cameraPreview.layer.addSublayer(previewLayer)
cameraPreview.addGestureRecognizer(UITapGestureRecognizer(target: self, action:"saveToCamera:"))
view.addSubview(cameraPreview)
}
}
for device in devices {
if device.position == AVCaptureDevicePosition.Front {
println("selfie capable")
}
}
}
override func viewWillLayoutSubviews() {
if avCaptureVideoPreview != nil {
if (avCaptureVideoPreview!.connection.supportsVideoOrientation == true) {
avCaptureVideoPreview!.connection.videoOrientation = interfaceOrientationToVideoOrientation(UIApplication.sharedApplication().statusBarOrientation)
}
avCaptureVideoPreview!.bounds = CGRectMake(0.0, 0.0, view.bounds.size.width, view.bounds.size.height)
avCaptureVideoPreview!.position = CGPointMake(view.bounds.midX, view.bounds.midY)
avCaptureVideoPreview!.videoGravity = AVLayerVideoGravityResizeAspectFill
}
}
func interfaceOrientationToVideoOrientation(orientation: UIInterfaceOrientation) -> AVCaptureVideoOrientation {
switch orientation {
case UIInterfaceOrientation.Portrait:
return AVCaptureVideoOrientation.Portrait
case UIInterfaceOrientation.PortraitUpsideDown:
return AVCaptureVideoOrientation.PortraitUpsideDown
case UIInterfaceOrientation.LandscapeLeft:
return AVCaptureVideoOrientation.LandscapeLeft
case UIInterfaceOrientation.LandscapeRight:
return AVCaptureVideoOrientation.LandscapeRight
default:
return AVCaptureVideoOrientation.Portrait
}
}
func saveToCamera(sender: UITapGestureRecognizer) {
if let videoConnection = stillImageOutput.connectionWithMediaType(AVMediaTypeVideo) {
stillImageOutput.captureStillImageAsynchronouslyFromConnection(videoConnection) {
(imageDataSampleBuffer, error) -> Void in
var image = UIImage(data: AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer))
var orientation : UIImageOrientation?
switch UIApplication.sharedApplication().statusBarOrientation.rawValue {
case 1:
orientation = UIImageOrientation.Up
case 3:
orientation = UIImageOrientation.Left
case 4:
orientation = UIImageOrientation.Right
default:
orientation = UIImageOrientation.Up
}
let newImage = UIImage(CGImage: image?.CGImage, scale: 1.0, orientation: orientation!)
UIImageWriteToSavedPhotosAlbum(newImage, nil, nil, nil)
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
source
share