title: AVCAPTURE image orientation
body: <p><a href="https://i.stack.imgur.com/TbP2A.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TbP2A.png" alt="enter image description here"></a>I have a view controller which allows a user to take a picture. I am setting the avcapture bounds to be the bounds of a view on screen.</p>
  
  <p>Above this view I have a collection view. So users can capture multiple pictures, they are then added to the collection view above.</p>
  
  <p>I am having trouble with the correct orientation appearing in my preview above.</p>
  
  <p>Code is as follows:</p>
  
  <pre><code>@IBOutlet weak var imagePreviews: UICollectionView!
  @IBOutlet weak var imgPreview: UIView!
  
  var session: AVCaptureSession?
  var stillImageOutput: AVCaptureStillImageOutput?
  var videoPreviewLayer: AVCaptureVideoPreviewLayer?
  var images: [UIImage] = [UIImage]()
  
  var isLandscapeLeft     : Bool = false
  var isLandscapeRight    : Bool = false
  var isPortrait          : Bool = false
  var isPortraitUpsideDown: Bool = false
  
  @IBAction func capture(_ sender: UIButton)
      {
      if let videoConnection = stillImageOutput?.connection(withMediaType: AVMediaTypeVideo)
              {
                  stillImageOutput?.captureStillImageAsynchronously(from: videoConnection, completionHandler: { (sampleBuffer, error) in
                      if sampleBuffer != nil {
                          if let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer), let dataProvider = CGDataProvider(data: imageData as CFData), let cgImageRef = CGImage(jpegDataProviderSource: dataProvider, decode: nil, shouldInterpolate: true, intent: CGColorRenderingIntent.defaultIntent)
                          {
                              var image: UIImage
  
                              if (self.isLandscapeLeft || self.isLandscapeRight)
                              {
                                  image = UIImage(cgImage: cgImageRef, scale: 1.0, orientation: self.isLandscapeLeft ? UIImageOrientation.left : UIImageOrientation.right)
                              } else {
                                  image = UIImage(cgImage: cgImageRef, scale: 1.0, orientation: self.isPortrait ? UIImageOrientation.up : UIImageOrientation.down)
                              }
  
                              image = self.fixOrientation(img: image)
                              self.images.append(image)
  
  
                              DispatchQueue.main.async {
                                  self.imagePreviews.reloadData()
                              }
                          }
                      }
                  })
              }
          }
     } 
  
  override func viewDidLayoutSubviews() {
  
      super.viewDidLayoutSubviews()
  
      if let connection =  self.videoPreviewLayer?.connection  {
  
          let currentDevice: UIDevice = UIDevice.current
  
          let orientation: UIDeviceOrientation = currentDevice.orientation
  
          let previewLayerConnection : AVCaptureConnection = connection
  
          if (previewLayerConnection.isVideoOrientationSupported) {
  
              switch (orientation) {
              case .portrait:
  
                  isLandscapeLeft      = false
                  isLandscapeRight     = false
                  isPortrait           = true
                  isPortraitUpsideDown = false
                  updatePreviewLayer(layer: previewLayerConnection, orientation: .portrait)
                  break
  
              case .landscapeRight:
  
                  isLandscapeLeft      = false
                  isLandscapeRight     = true
                  isPortrait           = false
                  isPortraitUpsideDown = false
                  updatePreviewLayer(layer: previewLayerConnection, orientation: .landscapeRight)
                  break
  
              case .landscapeLeft:
  
                  isLandscapeLeft      = true
                  isLandscapeRight     = false
                  isPortrait           = false
                  isPortraitUpsideDown = false
                  updatePreviewLayer(layer: previewLayerConnection, orientation: .landscapeLeft)
                  break
              case .portraitUpsideDown:
  
                  isLandscapeLeft      = false
                  isLandscapeRight     = false
                  isPortrait           = false
                  isPortraitUpsideDown = true
                  updatePreviewLayer(layer: previewLayerConnection, orientation: .portraitUpsideDown)
                  break
              default:
  
                  updatePreviewLayer(layer: previewLayerConnection, orientation: .portrait)
  
                  break
              }
          }
      }
  }
  
  func fixOrientation(img: UIImage) -&gt; UIImage {
          if (img.imageOrientation == .up) {
              return img
          }
  
          UIGraphicsBeginImageContextWithOptions(img.size, false, img.scale)
          let rect = CGRect(x: 0, y: 0, width: img.size.width, height: img.size.height)
          img.draw(in: rect)
  
          let normalizedImage = UIGraphicsGetImageFromCurrentImageContext()!
          UIGraphicsEndImageContext()
  
          return normalizedImage
      }
  </code></pre>