title: save a rotated picture (swift3)
body: <p><strong>picture without rotation</strong></p>
  
  <p><a href="https://i.stack.imgur.com/2Cih3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2Cih3.png" alt="picture without rotation"></a></p>
  
  <p><strong>picture with rotation</strong></p>
  
  <p><a href="https://i.stack.imgur.com/W2FQY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/W2FQY.png" alt="picture with rotation"></a></p>
  
  <p>All i want to do is take photos saved with the rotation. Exactly like the picture with rotation using a custom camera built with avfoundation. Right now my code takes photos and saves them to the photo gallery just like the picture without rotation. </p>
  
  <pre><code>    import UIKit
  import AVFoundation
  
  
  class ViewController: UIViewController, AVCapturePhotoCaptureDelegate {
  @IBOutlet var cameraDisplay: UIView!
  
  var captureSession : AVCaptureSession!
  var cameraOutput : AVCapturePhotoOutput!
  var previewLayer : AVCaptureVideoPreviewLayer!
  
  override func viewDidLoad() {
      super.viewDidLoad()
      // Do any additional setup after loading the view, typically from a nib.
      captureSession = AVCaptureSession()
      captureSession.sessionPreset = AVCaptureSessionPresetPhoto
      cameraOutput = AVCapturePhotoOutput()
  
  
      let device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
  
      if let input = try? AVCaptureDeviceInput(device: device) {
          if (captureSession.canAddInput(input)) {
              captureSession.addInput(input)
              if (captureSession.canAddOutput(cameraOutput)) {
                  captureSession.addOutput(cameraOutput)
  
  
                  previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
                  previewLayer.frame = CGRect(x: 10, y: 10, width: 700, height: 700)
  
  
  
  
                  cameraDisplay.layer.addSublayer(previewLayer)
                  captureSession.startRunning()
  
              }}}}
  
  
              func image(_ image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer) {
                  if let error = error {
                      // we got back an error!
                      let ac = UIAlertController(title: "Save error", message: error.localizedDescription, preferredStyle: .alert)
                      ac.addAction(UIAlertAction(title: "OK", style: .default))
                      present(ac, animated: true)
                  } else {
  
                      let ac = UIAlertController(title: "Image Saved!", message: "Your image has been saved to your photos.", preferredStyle: .alert)
                      ac.addAction(UIAlertAction(title: "OK", style: .default))
                      present(ac, animated: true)
                  }
              }
  
  func capture(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhotoSampleBuffer photoSampleBuffer: CMSampleBuffer?, previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) {
      if let sampleBuffer = photoSampleBuffer,
          let previewBuffer = previewPhotoSampleBuffer,
  
  
          let dataImage = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: sampleBuffer, previewPhotoSampleBuffer: previewBuffer){
  
  
          let dataProvider = CGDataProvider(data: dataImage as CFData)
          let cgImageRef: CGImage! = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: .defaultIntent)
          let imagea = UIImage(cgImage: cgImageRef, scale: 1.0, orientation: UIImageOrientation.right)
  
          UIImageWriteToSavedPhotosAlbum(imagea, self, #selector(self.image(_:didFinishSavingWithError:contextInfo:)), nil)
  
  
  
      }}
  
  
  
  @IBAction func TakePhoto(_ sender: Any) {
      let settings = AVCapturePhotoSettings()
      let previewpixel = settings.availablePreviewPhotoPixelFormatTypes.first!
      let previewformat = [
          kCVPixelBufferPixelFormatTypeKey as String: previewpixel, kCVPixelBufferWidthKey as String: 160,
          kCVPixelBufferHeightKey as String : 160]
  
      settings.previewPhotoFormat = previewformat
      cameraOutput.capturePhoto(with: settings, delegate: self)
      }}
  </code></pre>