mafioso
mafioso

Reputation: 1632

Camera just works in Portrait mode

My camera looks like this in portrait mode:

enter image description here

and like that in landscape mode:

enter image description here

So my questions are:

  1. how can I set the previewLayer there in fullscreen in landscape mode?
  2. why doesn't the camera rotate when i rotate the device? It stays always in portrait mode

Upvotes: 0

Views: 1135

Answers (2)

Mohamad Sheikh
Mohamad Sheikh

Reputation: 283

You should set your AVCaptureSession orientation every time device's orientation is changed.

Add a Notification observer to observe any change in device orientation then set AVCaptureSession orientation according to device orientation.

Obj-C:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(orientationChanged)
                                             name:UIDeviceOrientationDidChangeNotification
                                           object:nil];
-(void) orientationChanged
{
    if (self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
        [_previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationPortraitUpsideDown];

    if (self.interfaceOrientation == UIInterfaceOrientationPortrait)
        [_previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationPortrait];

    if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
        [_previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationLandscapeLeft];

    if (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight)
        [_previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationLandscapeRight];
}

Swift 2:

Add observer somewhere, for example in viewDidLoad method.

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(orientationChanged), name: UIDeviceOrientationDidChangeNotification, object: nil)

Then:

func orientationChanged(){
    let deviceOrientation = UIDevice.currentDevice().orientation

    if deviceOrientation == UIDeviceOrientation.PortraitUpsideDown {
        previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.PortraitUpsideDown
    }
    else if deviceOrientation == UIDeviceOrientation.Portrait {
        previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.Portrait
    }
    else if deviceOrientation == UIDeviceOrientation.LandscapeLeft {
        previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.LandscapeLeft
    }
    else if deviceOrientation == UIDeviceOrientation.LandscapeRight {
        previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.LandscapeRight
    }
}

You can also provide any of your code or look at this question which has been asked before it may help. AVCaptureSession with multiple orientations issue

Upvotes: 2

Andrew Kirna
Andrew Kirna

Reputation: 1396

Make sure the phone's "portrait orientation lock" is turned off. And make sure the app allows landscape orientations in the project settings > general tab.

Upvotes: 0

Related Questions