2

I'm developing an iOS app with latest SDK.

I'm developing an camera app which only supports LandscapeRight orientation.

On Main ViewController I have added a UIView for AVCaptureVideoPreviewLayer. In other words, main ViewController I have main view, and another view, called videoPreviewView, for AVCaptureVideoPreviewLayer.

I set up AVCaptureVideoPreviewLayer on viewWillAppear: method:

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self setUpVideo];
}

- (void)setUpVideo
{
    NSLog(@"Set up video");
    if (DataExchanger.cameraManager != nil)
    {
        UIView *view = [self videoPreviewView];
        CALayer *viewLayer = [view layer];
        [viewLayer setMasksToBounds:YES];

        AVCaptureVideoPreviewLayer *newCaptureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:DataExchanger.cameraManager.captureSession];
        CGRect bounds = [view bounds];
        NSLog(@"BOUNDS: %@", NSStringFromCGRect(bounds));

        [newCaptureVideoPreviewLayer setFrame:bounds];
        [newCaptureVideoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];

        [viewLayer insertSublayer:newCaptureVideoPreviewLayer below:[[viewLayer sublayers] objectAtIndex:0]];
    }
}

I get this log:

BOUNDS: {{0, 0}, {320, 480}}

But UIInterfaceOrientation is UIInterfaceOrientationLandscapeRight and with that orientation I would get these values:

BOUNDS: {{0, 0}, {480, 320}}

And another problem is that I see video rotated -90 degrees.

I have found this stackoverflow question, but - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation method is never triggered.

What's happening? How can I solved this problem?

Community
  • 1
  • 1
VansFannel
  • 45,055
  • 107
  • 359
  • 626

1 Answers1

-1
  1. The default AVCaptureVideoPreviewLayer object orientation is landscape!
  2. The orientation of the AVCaptureVideoPreviewLayer does not change with the orientation of the device. You have to change it manually.
  3. To change it manually, you need to call the connection of the preview layer and rotate it:

iOS6:

 AVCaptureConnection *previewLayerConnection=self.newCaptureVideoPreviewLayer.connection;
 if ([previewLayerConnection isVideoOrientationSupported])
   [previewLayerConnection setVideoOrientation:AVCaptureVideoOrientationLandscapeRight];

< iOS6:

if ([self.newCaptureVideoPreviewLayer isOrientationSupported])   
[self.newCaptureVideoPreviewLayer setOrientation:AVCaptureVideoOrientationLandscapeRight];
Khaled Barazi
  • 8,681
  • 6
  • 42
  • 62
  • I'm working with the latest SDK and I'm supporting iOS 5.0 and above. Which version from your answer do I have to use? But I think your answer doesn't resolved my problem because now I'm using the second option and I'm having this problem. – VansFannel Mar 18 '13 at 07:36
  • If someone is going to downvote 3 of my answers randomly, 3 months after posting; at a minimum, please post a comment with the reason (if there is one). – Khaled Barazi May 20 '14 at 11:24