A.Vila
A.Vila

Reputation: 1486

Change camera and device orientation

I have an application that is basically a view and when the user click a button, camera visualization starts.

I want to allow all orientations when the camera isn't showed, but when the camera is showed, I need to force the application to portrait mode because if it isn't on Portrait, the video is rotated. When the camera is closed, the app may rotate again.

Do you know if I can solve the problem with video orientation?

Or how can I force the app to portrait mode? I know that on earlier ios version, you can use [UIDevice setOrientation:], but it's deprecated for lastest ios.

How can I do this for ios 5 and ios 6?

I've tried with:

[self presentViewController:[UIViewController new] animated:NO 
completion:^{ [self dismissViewControllerAnimated:NO completion:nil]; }]; 

And in shouldAutorotateToInterfaceOrientation method:

if (state == CAMERA) {
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }else{
        return YES;
}

It works ok and force the app to portrait. But when the camera is closed, it doesn't work properly, it doesn't rotate well.

I mean, when the camera is closed this is what happens:

Do you know what could be the problem?

Thanks in advance.

Upvotes: 1

Views: 4396

Answers (2)

A.Vila
A.Vila

Reputation: 1486

I think I've found a solution for the problem with change camera and device orientation at the same time.

I call this code when I initialize the camera, and also in shouldAutorotateToInterfaceOrientation method, where I allow all orientations.

 AVCaptureVideoOrientation newOrientation;

    UIInterfaceOrientation deviceOrientation = [UIApplication sharedApplication].statusBarOrientation;

    NSLog(@"deviceOrientation %c",deviceOrientation);

    switch (deviceOrientation)
    {
        case UIInterfaceOrientationPortrait:
            NSLog(@"UIInterfaceOrientationPortrait");
            newOrientation = AVCaptureVideoOrientationPortrait;
            break;
        case UIInterfaceOrientationLandscapeRight:
            NSLog(@"UIInterfaceOrientationLandscapeRight");
            newOrientation = AVCaptureVideoOrientationLandscapeRight;
            break;
        case UIInterfaceOrientationLandscapeLeft:
            NSLog(@"UIInterfaceOrientationLandscapeLeft");
            newOrientation = AVCaptureVideoOrientationLandscapeLeft;
            break;
        default:
            NSLog(@"default");
            newOrientation = AVCaptureVideoOrientationPortrait;
            break;
    }

    if ([self.prevLayer respondsToSelector:@selector(connection)]){
        if ([self.prevLayer.connection isVideoOrientationSupported]){
            self.prevLayer.connection.videoOrientation = newOrientation;
        }else{
            NSLog(@"NO respond to selector connection");
        }
    }else{


if ([self.prevLayer isOrientationSupported]){
        self.prevLayer.orientation = newOrientation;
    }else{
        NSLog(@"NO isOrientationSupported");
    }

}

Upvotes: 3

Nimit Parekh
Nimit Parekh

Reputation: 16864

Please Try Following code for the Orientation so i think your problem may be solved.

   - (void)encodeVideoOrientation:(NSURL *)anOutputFileURL
    {
    CGAffineTransform rotationTransform;
    CGAffineTransform rotateTranslate;
    CGSize renderSize;

    switch (self.recordingOrientation)
    {
        // set these 3 values based on orientation

    }


    AVURLAsset * videoAsset = [[AVURLAsset alloc]initWithURL:anOutputFileURL options:nil];

    AVAssetTrack *sourceVideoTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
    AVAssetTrack *sourceAudioTrack = [[videoAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];

    AVMutableComposition* composition = [AVMutableComposition composition];

    AVMutableCompositionTrack *compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
    [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration)
                                   ofTrack:sourceVideoTrack
                                    atTime:kCMTimeZero error:nil];
    [compositionVideoTrack setPreferredTransform:sourceVideoTrack.preferredTransform];

    AVMutableCompositionTrack *compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio
                                                                                preferredTrackID:kCMPersistentTrackID_Invalid];
    [compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration)
                                   ofTrack:sourceAudioTrack
                                    atTime:kCMTimeZero error:nil];



    AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
    AVMutableVideoCompositionLayerInstruction *layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:compositionVideoTrack];
    [layerInstruction setTransform:rotateTranslate atTime:kCMTimeZero];

    AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];
    videoComposition.frameDuration = CMTimeMake(1,30);
    videoComposition.renderScale = 1.0;
    videoComposition.renderSize = renderSize;
    instruction.layerInstructions = [NSArray arrayWithObject: layerInstruction];
    instruction.timeRange = CMTimeRangeMake(kCMTimeZero, videoAsset.duration);
    videoComposition.instructions = [NSArray arrayWithObject: instruction];

    AVAssetExportSession * assetExport = [[AVAssetExportSession alloc] initWithAsset:composition
                                                                          presetName:AVAssetExportPresetMediumQuality];

    NSString* videoName = @"export.mov";
    NSString *exportPath = [NSTemporaryDirectory() stringByAppendingPathComponent:videoName];

    NSURL * exportUrl = [NSURL fileURLWithPath:exportPath];

    if ([[NSFileManager defaultManager] fileExistsAtPath:exportPath])
    {
        [[NSFileManager defaultManager] removeItemAtPath:exportPath error:nil];
    }

    assetExport.outputFileType = AVFileTypeMPEG4;
    assetExport.outputURL = exportUrl;
    assetExport.shouldOptimizeForNetworkUse = YES;
    assetExport.videoComposition = videoComposition;

    [assetExport exportAsynchronouslyWithCompletionHandler:
     ^(void ) {
         switch (assetExport.status)
         {
             case AVAssetExportSessionStatusCompleted:
                 //                export complete
                 NSLog(@"Export Complete");
                 break;
             case AVAssetExportSessionStatusFailed:
                 NSLog(@"Export Failed");
                 NSLog(@"ExportSessionError: %@", [assetExport.error localizedDescription]);
                 //                export error (see exportSession.error)
                 break;
             case AVAssetExportSessionStatusCancelled:
                 NSLog(@"Export Failed");
                 NSLog(@"ExportSessionError: %@", [assetExport.error localizedDescription]);
                 //                export cancelled
                 break;
         }
     }];

    }

Hope this helps you.!!

Upvotes: 1

Related Questions