Swift UI 相机显示纵向视图,但图像变成横向 [英] Swift UI camera shows portrait view but image turns out landscape

查看:58
本文介绍了Swift UI 相机显示纵向视图,但图像变成横向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 swiftUI 应用程序中使用相机预览和照片,我可以显示预览并拍摄照片,但比例出现错误.预览屏幕看起来不错,但一旦发生捕获,它就会扭曲并且不会保持纵向.

I'm trying to get camera preview and photos working in a swiftUI app, I can display the preview and snap a photo but proportions are coming out wrong. The preview screen looks good, but as soon as the capture happens, it gets distorted and doesn't stay in portrait.

预览屏幕如下所示:

一些代码:

class CameraController: UIViewController {
    //...
    override func viewDidLoad() {
        //...
        // Setup preview layer
        previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        previewLayer?.videoGravity = .resizeAspectFill
        previewLayer?.connection?.videoOrientation = .portrait
        previewLayer?.frame = view.frame
        view.layer.insertSublayer(previewLayer!, at: 0)
        //...
    }
}

但是在我拍下照片后,我在 VStack 中显示的 Image 看起来像:

But after I snap a photo, the Image I'm displaying in a VStack looks like:

代码是:

VStack {
    Image(uiImage: self.inputImage!)
        .resizable()
        .aspectRatio(contentMode: .fit)
        .edgesIgnoringSafeArea(.all)
}

我使用 .fit 是因为我想看到整个事情,但结果却全错了.另外,这是我在 viewDidLoad 中发生的相机设置代码:

I'm using .fit because I want to see the whole thing, and it's just coming out all wrong. Also, here's my camera setup code happening inside viewDidLoad:

captureSession.beginConfiguration()

// Get device
captureDevice = AVCaptureDevice.default(.builtInDualCamera, for: .video, position: .back)

do {
    // Create input
    let captureDeviceInput = try AVCaptureDeviceInput(device: captureDevice!)
    captureSession.addInput(captureDeviceInput)

    // Create output
    photoOutput = AVCapturePhotoOutput()
    captureSession.sessionPreset = .photo
    photoOutput?.setPreparedPhotoSettingsArray(
        [AVCapturePhotoSettings(format: [AVVideoCodecKey: AVVideoCodecType.jpeg])],
        completionHandler: nil
    )
    captureSession.connections.first?.videoOrientation = .portrait // tested with and and without this line
    captureSession.addOutput(photoOutput!)
    captureSession.commitConfiguration()
} catch {
    print(error)
}

非常感谢任何帮助!

推荐答案

它仅在横向返回,因为使用应用内相机拍摄的图像具有方向.您需要对其进行归一化并显示出来.

It returns only on landscape because the image which was taken with in-app camera has an orientation. You need to normalize it and display it.

我使用

func fixOrientation(img:UIImage) -> UIImage {

  if (img.imageOrientation == UIImageOrientation.Up) { 
      return img;
  }

  UIGraphicsBeginImageContextWithOptions(img.size, false, img.scale);
  let rect = CGRect(x: 0, y: 0, width: img.size.width, height: img.size.height)
  img.drawInRect(rect)

  let normalizedImage : UIImage = UIGraphicsGetImageFromCurrentImageContext()
  UIGraphicsEndImageContext();
  return normalizedImage;

}

来自这里的原始答案:对相机拍摄的照片进行标准化

此外,我还对 ImagePickerManager 的复制和粘贴解决方案进行了一些更改:

Also I just did a few changes to copy and paste solution for the ImagePickerManager:

ImagePickerManager

这篇关于Swift UI 相机显示纵向视图,但图像变成横向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆