如何在捕获后改变图像的方向?

时间:2017-03-21 03:44:55

标签: ios swift

这是我在我的自定义相机中用于拍摄照片的代码,但它正确捕捉照片但是如果假设用户旋转设备(但相机仍然像iphone相机那样的肖像)并拍照我想要那张照片中的图片。我这样做?

        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

1 个答案:

答案 0 :(得分:3)

在videoConnection初始化之后添加以下代码,你很高兴

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
                }