简体   繁体   English

更改相机和设备方向

[英]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. 我希望在未显示相机时允许所有方向,但是当显示相机时,我需要强制应用程序进入纵向模式,因为如果它不在Portrait上,则视频会旋转。 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. 我知道在早期的ios版本中,您可以使用[UIDevice setOrientation:] ,但是对于最新的ios,它已被弃用。

How can I do this for ios 5 and ios 6? 我怎么能为ios 5和ios 6做到这一点?

I've tried with: 我尝试过:

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

And in shouldAutorotateToInterfaceOrientation method: 并在shouldAutorotateToInterfaceOrientation方法:

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: 我的意思是,当相机关闭时,会发生以下情况:

  • The app is on portrait 该应用程序是在肖像上
  • If I try to rotate the app, the device is rotated but not the application. 如果我尝试旋转应用程序,则会旋转设备但不会旋转应用程序。 I can see that status bar of ipad with info about time, battery, etc is rotated but the app not. 我可以看到ipad的状态栏有关时间,电池等的信息被旋转但应用程序没有。
  • If I go to portrait again and then I rotate the device, it works ok. 如果我再次进入肖像然后旋转设备,它就可以了。

Do you know what could be the problem? 你知道可能是什么问题吗?

Thanks in advance. 提前致谢。

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. 我在初始化相机时调用此代码,并在shouldAutorotateToInterfaceOrientation方法中调用此代码,其中我允许所有方向。

 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");
    }

}

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.!! 希望这对你有所帮助。!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM