Landscape in AvCam iOS 6

3.3k Views Asked by At

I am new to iOS, trying to create custom camera using AvCam. I am having trouble getting landscape orientation preview -- it rotates the view 90 degree clockwise and shows it on a half screen.

I get this message --

WARNING: -[ setOrientation:] is deprecated.

Please use AVCaptureConnection's -setVideoOrientation:

AVCaptureConnection already sets orientation, so I have no clue what am I supposed else.

I know this question was asked lots of times for previous versions of iOS (4,5), but non of those techniques/codes worked for me (iOS 6).

Original code (did not make any changes from Apple)

if ([self captureManager] == nil) {
    AVCamCaptureManager *manager = [[AVCamCaptureManager alloc] init];
    [self setCaptureManager:manager];
    [manager release];

    [[self captureManager] setDelegate:self];

    if ([[self captureManager] setupSession]) {
        // Create video preview layer and add it to the UI
        AVCaptureVideoPreviewLayer *newCaptureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:[[self captureManager] session]];


        UIView *view = [self videoPreviewView];
        CALayer *viewLayer = [view layer];
        [viewLayer setMasksToBounds:YES];

        CGRect bounds = [view bounds];
        [newCaptureVideoPreviewLayer setFrame:bounds];

        if ([newCaptureVideoPreviewLayer isOrientationSupported]) {
            [newCaptureVideoPreviewLayer setOrientation:AVCaptureVideoOrientationPortrait];
        }



        [newCaptureVideoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];

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

        [self setCaptureVideoPreviewLayer:newCaptureVideoPreviewLayer];

AVCaptureConnection chunk:

-(void)startRecordingWithOrientation:(AVCaptureVideoOrientation)videoOrientation; {
AVCaptureConnection *videoConnection = [AVCamUtilities connectionWithMediaType:AVMediaTypeVideo fromConnections:[[self movieFileOutput] connections]];
if ([videoConnection isVideoOrientationSupported])
    [videoConnection setVideoOrientation:videoOrientation];

[[self movieFileOutput] startRecordingToOutputFileURL:[self outputFileURL] recordingDelegate:self];

}

2

There are 2 best solutions below

0
On

Last time i also stumbled upon this issue. I solved this problem by doing two things

  1. Getting the right orientation
    Replace

    if ([newCaptureVideoPreviewLayer isOrientationSupported]) {
        [newCaptureVideoPreviewLayer setOrientation:AVCaptureVideoOrientationPortrait];
    }  
    

    With

    if ([newCaptureVideoPreviewLayer.connection isVideoOrientationSupported]) {  
        [newCaptureVideoPreviewLayer.connection setVideoOrientation:[UIDevice currentDevice].orientation];
    }
    
  2. Force the update of the video orientation during the initialization to catch video output in landscape mode by triggering - (void)deviceOrientationDidChange manually within AVCaptureManager.m

    I added it to:

    - (BOOL) setupSession
    {
        BOOL success = NO;
    
        ...
    
        AVCamRecorder *newRecorder = [[AVCamRecorder alloc] initWithSession:[self session] outputFileURL:self.lastOutputfileURL];
        [newRecorder setDelegate:self];
    
        [self performSelector:@selector(deviceOrientationDidChange)];
    
        ...
    
        return success;
    }
    
1
On

So, here's a workaround, but I am sure that there must be better solution than this. I got part of the code from this question :

iPhone App - Show AVFoundation video on landscape mode

But had to adjust frame for every orientation in order to make it work on iOS6 (and it still shows a warning):


-(void)willAnimateRotationToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

[CATransaction begin];
if (toInterfaceOrientation==UIInterfaceOrientationLandscapeLeft){
    captureVideoPreviewLayer.orientation = UIInterfaceOrientationLandscapeLeft;
    captureVideoPreviewLayer.frame = CGRectMake(0, 0, 480, 320);

} else if (toInterfaceOrientation==UIInterfaceOrientationPortrait){
    captureVideoPreviewLayer.orientation = UIInterfaceOrientationPortrait;
    captureVideoPreviewLayer.frame = CGRectMake(0, 0, 320, 480);

} else if (toInterfaceOrientation==UIInterfaceOrientationLandscapeRight){
    captureVideoPreviewLayer.orientation = UIInterfaceOrientationLandscapeRight;
    captureVideoPreviewLayer.frame = CGRectMake(0, 0, 480, 320);

}
[CATransaction commit];
[super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];

}