1

I am following this link here How to apply filter to Video real-time using Swift and for whatever reason the UIImageOrientation is rotated to the left 90 degrees.I have tried to rectify this by setting the orientation to Up but it still appears the same. Does anyone have any idea why this is? I'm not sure if it is the image, previewlayer or image view that is causing this

Here is the code:

import UIKit
import AVFoundation
import CoreMedia



     let noirFilter = CIFilter(name: "CIPhotoEffectNoir")!

     let sepiaFilter = CIFilter(name: "CISepiaTone")!

     let vignetteEffect = CIFilter(name: "CIVignetteEffect")!



     let Filters = [noirFilter,sepiaFilter,vignetteEffect]




    class TestViewController:UIViewController,AVCaptureVideoDataOutputSampleBufferDelegate {

        @IBOutlet weak var imageView: UIImageView!
        @IBOutlet weak var Photo: UIButton!
        var sessionQueue: dispatch_queue_t!
        var stillImageOutput: AVCaptureStillImageOutput?
        var videoDeviceInput: AVCaptureDeviceInput?

        var buttonTapped = false

        override func viewDidLoad() {
            super.viewDidLoad()




           sessionQueue = dispatch_queue_create("com.bradleymackey.Backchat.sessionQueue",DISPATCH_QUEUE_SERIAL)


            let captureSession = AVCaptureSession()
            captureSession.sessionPreset = AVCaptureSessionPresetPhoto

            let backCamera = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
            var videoDeviceInput: AVCaptureVideoDeviceInput?

            do
            {
                let input = try AVCaptureDeviceInput(device: backCamera)

                captureSession.addInput(input)
                 self.videoDeviceInput = videoDeviceInput
            }
            catch
            {
                print("can't access camera")
                return
            }



            // although we don't use this, it's required to get captureOutput invoked
            let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)







            view.layer.addSublayer(previewLayer)

             imageView.contentMode = UIViewContentMode.ScaleAspectFill

            let videoOutput = AVCaptureVideoDataOutput()

            videoOutput.setSampleBufferDelegate(self, queue: dispatch_queue_create("sample buffer delegate", DISPATCH_QUEUE_SERIAL))
            if captureSession.canAddOutput(videoOutput)
            {
                captureSession.addOutput(videoOutput)
            }




            captureSession.startRunning()

            let stillImageOutput: AVCaptureStillImageOutput = AVCaptureStillImageOutput()
            if captureSession.canAddOutput(stillImageOutput) {
                stillImageOutput.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]
                captureSession.addOutput(stillImageOutput)

                self.stillImageOutput = stillImageOutput



        }
        }

        override func viewWillLayoutSubviews() {




        }




        func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!)
        {
             let filter = Filters[1]



            let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
            let cameraImage = CIImage(CVPixelBuffer: pixelBuffer!)

            filter.setValue(cameraImage, forKey: kCIInputImageKey)

            let filteredImage = UIImage(CIImage: filter.valueForKey(kCIOutputImageKey) as! CIImage!)

            var newImage = UIImage(CIImage: filter.valueForKey(kCIOutputImageKey) as! CIImage!, scale:1.0, orientation: .Up)

            dispatch_async(dispatch_get_main_queue())
            {
                self.imageView.image = newImage
            }

        }

        @IBAction func snapStill(sender: UIButton) {
            print("snapStillImage")



            let previewController = PreviewViewController(nibName: "PreviewViewController", bundle: nil)
                        previewController.media = Media.Photo(image: imageView.image!)
                        previewController.isFrontCamera = false 

                        self.presentViewController(previewController, animated: true, completion: nil)

        }


        override func viewDidLayoutSubviews()
        {




        }


        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    }
Community
  • 1
  • 1
Onicha21
  • 141
  • 2
  • 11

1 Answers1

0

The reason for this is that the orientation is not set. You need to set the orientation in captureOutput()

func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) 
{
connection.videoOrientation = .portrait
...

This should solve your problem.