【问题标题】:How to change orientation of image after capture?拍摄后如何改变图像的方向?
【发布时间】:2017-03-21 03:44:55
【问题描述】:

这是我在自定义相机中用于拍摄照片的代码,但它可以正确拍摄照片,但如果假设用户旋转设备(但相机仍会像 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]

【问题讨论】:

标签: ios swift


【解决方案1】:

在 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
                }

【讨论】:

  • 天啊! landscapeLeft landscapeRight 方向不匹配!我猜我的代码中的不一致让我感觉好一点。
【解决方案2】:

在拍摄图像之前设置方向

// 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-20
    • 2021-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多