1

我在此处关注此链接如何使用 Swift 对视频实时应用过滤器,无论出于何种原因,UIImageOrientation 都向左旋转了 90 度。我试图通过将方向设置为向上来纠正这一点,但它仍然看起来相同。有谁知道这是为什么?我不确定是图像、预览层还是图像视图导致了这种情况

这是代码:

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.
        }
    }
4

1 回答 1

0

原因是没有设置方向。您需要在 captureOutput() 中设置方向

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

这应该可以解决您的问题。

于 2019-02-23T07:30:23.717 回答