imaginary-cloud / CameraManager

Simple Swift class to provide all the configurations you need to create custom camera view in your app
MIT License
1.37k stars 324 forks source link

Confused about orientation #242

Open Vweston opened 3 years ago

Vweston commented 3 years ago

I’m confused. I created a new project. Added Swift package to project. Added a cameraView & label to storyboard. Build & run on device (iPad Pro 10.5” - iPadOS 14) in Portrait. Everything looks great. Camera view is correct. Rotate left & image is now displaying 90 degrees right. Rotate device back to portrait & camera image now incorrect there as well.

if I start out in landscape right. Then cameraView is rotated 90 degrees right, but is correct when back in portrait. I can only get it to correctly view in portrait mode no matter what settings I try. See images where the “This is a Label is always correctly displayed above cameraView.

here is my ViewController // // ViewController.swift // BasicCameraManager // // Created by Vincent Weston on 1/20/21. //

import UIKit import CameraManager

class ViewController: UIViewController {

@IBOutlet weak var cameraView: UIView!

let cameraManager = CameraManager()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    //cameraManager.shouldKeepViewAtOrientationChanges = false
    //cameraManager.shouldRespondToOrientationChanges = false

    cameraManager.addPreviewLayerToView(self.cameraView)
}

}

Start in Portrait IMG_5991

rotated IMG_5992

Started in landscapeRight IMG_5993

Rotated back to portrait IMG_5994

any help would be really great.👍

murraysagal commented 3 years ago

@Vweston Same here. Did you ever find a solution for this?

murraysagal commented 3 years ago

I have the same issue. Here are the details:

I'm running on two devices. An iPhone X with 14.4.2 and an iPad Mini (5th generation) also on 14.4.2. I get the same results on either device.

  1. Start in Portrait. Everything is as expected.
  2. Rotate to landscape, either way. Two things happen. The view is rotated 90 deg off from what it should be. And the view is cropped towards the home button.
  3. Rotate back to portrait. The view remains cropped and is still 90 deg off.

I notice that the default value in the source for shouldKeepViewAtOrientationChanges is false. So I create a CameraManager subclass and override shouldKeepViewAtOrientationChanges so it returns true. In ViewController I change it to let cameraManager = MyCameraManager()

Then run:

  1. Start in Portrait. Everything is as expected.
  2. Rotate to landscape. Same result as before. The view crops and is off by 90 deg.
  3. Rotate back to portrait. This is correct now. Proper orientation and not cropped.

I haven't found a solution for the orientation issues but viewWillTransition() helped with the cropping.

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)
    cameraManager.addPreviewLayerToView(view)
}

I don't know if that's the best solution for that.

Can you provide any help on the orientation issues?

qhu91it commented 3 years ago

For my case, I setting like that

cameraManager.shouldKeepViewAtOrientationChanges = false
cameraManager.shouldRespondToOrientationChanges = true

And in where do the update UI when rotation change like viewWillTransition for example (me is updateUIView because I code SwiftUI), call resetOrientation function

DispatchQueue.main.async {
    self.cameraManager.resetOrientation()
}
joopvanduin commented 2 years ago

qhu91it Thank you that solved my problem