Reputation: 1431
I'm using AVFoundation to create a video camera UI. The containing viewcontroller will only be displayed in LandscapeRight orientation. When changing the output orientation via the preview layer, it displays correctly:
self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];
self.previewLayer.orientation = AVCaptureVideoOrientationLandscapeRight;
[self.displayContainer.layer addSublayer:self.previewLayer];
However, I realize that the orientation property of AVCaptureVideoPreviewLayer is deprecated in favor of AVCaptureConnection.videoOrientation. When using that instead:
self.previewLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight;
The preview layer doesn't appear to change orientation at all, even when trying different options for the orientation. Is there another step I am missing in order to do this the recommended way?
EDIT:
Below are the autorotate methods present in the view controller:
-(BOOL)shouldAutorotate
{
return NO;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeRight;
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}
SOLUTION:
This is solved both by apply all of the advice in @Spectravideo328's answer, and by adding the preview layer on to the ViewControllers view layer, as opposed to the displayContainer subview.
Upvotes: 4
Views: 2166
Reputation: 8741
Your problem is not an AvCaptureVideoPreviewLayer issue but rather a CALayer issue in general (which previewLayer is a subclass of).
All CALayers needs their frames set: in your case, I would just add to your code:
self.previewlayer.frame=self.view.bounds.
It is critical that you use self.view.bounds and not self.view.frame so that the previewlayer dimensions rotate (adjust) if the device is rotated.
Hope this helps.
Update: This is from the supportedInterfaceOrientations help description:
When the user changes the device orientation, the system calls this method on the root view controller or the topmost presented view controller that fills the window. If the view controller supports the new orientation, the window and view controller are rotated to the new orientation. This method is only called if the view controller’s shouldAutorotate method returns YES.
Update your shouldAutorotate to always return YES.
Upvotes: 1