1

This is code which i used in my custom camera for capture photo but it capturing photo correctly but if suppose user rotate device(but camera will still in portrait like iphone camera) and take picture i want that picture in portrait.how can i do that?

        let stillImageOutput = self._getStillImageOutput()

        if let videoConnection = stillImageOutput.connection(withMediaType: AVMediaTypeVideo) {

     //   videoConnection.videoOrientation = AVCaptureVideoOrientation.portrait

        stillImageOutput.captureStillImageAsynchronously(from: videoConnection , completionHandler: { [weak self] sample, error in

            if let error = error {
                DispatchQueue.main.async(execute: {
                    self?._show(NSLocalizedString("Error", comment:""), message: error.localizedDescription)
                })
                imageCompletion(nil,error as NSError?)
                return
            }

            let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sample)

             var capturedImage = UIImage(data: imageData!)[![This how photo showing if i capture by rotating Device][1]][1]

This image i want in portrait

Jay Patel
  • 343
  • 2
  • 19
  • Possible duplicate of [How to flip UIImage horizontally with Swift?](http://stackoverflow.com/questions/24965638/how-to-flip-uiimage-horizontally-with-swift) –  Mar 21 '17 at 03:45
  • Nope.it's different question. – Jay Patel Mar 21 '17 at 03:47
  • This might help. It's in Objective-C but you get the idea. http://stackoverflow.com/a/7932657/7005670 – Aleks Beer Mar 21 '17 at 04:16

2 Answers2

3

add below code after the initialization of videoConnection and you good to go

switch UIDevice.current.orientation {
                case .landscapeLeft:
                    videoConnection.videoOrientation = AVCaptureVideoOrientation.landscapeRight
                    break
                case .landscapeRight:
                    videoConnection.videoOrientation = AVCaptureVideoOrientation.landscapeLeft
                    break
                case .portrait:
                     videoConnection.videoOrientation = AVCaptureVideoOrientation.portrait
                    break
                case .portraitUpsideDown:
                    videoConnection.videoOrientation = AVCaptureVideoOrientation.portraitUpsideDown
                    break
                default:
                    videoConnection.videoOrientation = AVCaptureVideoOrientation.portrait
                    break
                }
Suhani Mendapara
  • 297
  • 1
  • 3
  • 10
  • OMG! The `landscapeLeft ` and `landscapeRight ` orientations don't match! Makes me feel a little better about inconsistencies in my code I guess. – Jon Vogel Jul 06 '18 at 17:33
-1

Set the orientation before capturing the image

// set the image orientation in output
  if let photoOutputConnection = self.photoOutput.connection(with: .video) {
        photoOutputConnection.videoOrientation = videoPreviewLayerOrientation! // set the orientation you want
    }

self.photoOutput.capturePhoto(with: photoSettings, delegate: photoCaptureProcessor) // capture image
YodagamaHeshan
  • 4,996
  • 2
  • 26
  • 36