2

picture without rotation

picture without rotation

picture with rotation

picture with rotation

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.

    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)
    }}
Adeel Miraj
  • 2,472
  • 3
  • 22
  • 32

2 Answers2

4

Change below line

UIImage(cgImage: (otherImage?.cgImage!)!, scale: CGFloat(1.0), orientation: .right)

to

UIImage(cgImage: (otherImage?.cgImage!)!, scale: CGFloat(1.0), orientation: .up)
Jasmeet Kaur
  • 1,538
  • 14
  • 16
  • do you know how I could the orientation depending on weather the device is in portrait or landscape? –  Apr 08 '17 at 20:41
  • do you want to know whether the device is in portrait or landscepae? – Jasmeet Kaur Apr 08 '17 at 20:53
  • right now it would be in portrait. But I want to do something along the lines if device is in portrait image orientation is right but if the device is in landscape image orientation is left. –  Apr 08 '17 at 21:00
  • check this http://stackoverflow.com/questions/25796545/getting-device-orientation-in-swift and please upvote above answer if you think the answer is correct. – Jasmeet Kaur Apr 08 '17 at 21:01
0

You can also call if let connection = cameraOutput.connection(withMediaType: AVMediaTypeVideo) { connection.videoOrientation = orientation } before calling cameraOutput.capturePhoto(). The variable orientation type is the enum AVCaptureVideoOrientation

adamfowlerphoto
  • 2,708
  • 1
  • 11
  • 24