2016-04-11 53 views
0

我有一个视图控制器,我添加了一个UIView全屏尺寸,在该UIView中我有AVCapturesession,可以帮助我捕捉照片,我的视图控制器在纵向打开良好模式,但在横向模式下突然打开。如何在横向模式下正确打开我的应用程序

的代码如下,

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.view.backgroundColor = [UIColor whiteColor]; 
    [[UIDevice currentDevice]beginGeneratingDeviceOrientationNotifications]; 
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(orientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil]; 
    self.camera.userInteractionEnabled = YES; 
    UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinchGesture:)]; 
    pinchGesture.delegate=self; 
    [self.camera addGestureRecognizer:pinchGesture]; 
    [self.navigationController setNavigationBarHidden:YES animated:YES]; 
} 

相机是UIView的是我的UIViewController的财产,

再次,

-(void) viewDidAppear:(BOOL)animated 
{ 
    AVCaptureSession *session = [[AVCaptureSession alloc] init]; 
    session.sessionPreset = AVCaptureSessionPresetMedium; 

    CALayer *viewLayer = self.camera.layer; 
    NSLog(@"viewLayer = %@", viewLayer); 
    captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session]; 

    captureVideoPreviewLayer.frame = self.camera.layer.bounds; 
    [self.camera.layer addSublayer: captureVideoPreviewLayer]; 
    device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 
    NSError *error = nil; 
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error]; 
    if (!input) { 
    // Handle the error appropriately. 
    NSLog(@"ERROR: trying to open camera: %@", error); 
    } 
    [session addInput:input]; 

    [session startRunning]; 
    stillImageOutput = [[AVCaptureStillImageOutput alloc] init]; 
    NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil]; 
    [stillImageOutput setOutputSettings:outputSettings]; 

    [session addOutput:stillImageOutput]; 

    isUsingFlash = NO; 
    isUsingFrontFacingCamera = NO; 
    effectiveScale = 1.0; 
} 

我的视图中打开错在横向模式一旦我旋转到肖像它会很好,再次旋转到风景它的作品很好,只是它不能正常启动在风景模式为什么?

这里我设置了根视图控制器,

sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; 
cameraFirstController = [sb instantiateViewControllerWithIdentifier:@"FirstViewController"]; 
cameraFirstController.delegate = self; 
nav = [[CustomNavigationController alloc]initWithRootViewController:cameraFirstController]; 
[self.viewController presentViewController:nav animated:YES completion:nil]; 
+0

你可以在设置rootViewController的地方添加代码。 –

+0

我在y编辑答案中添加了代码。请检查 –

回答

0

好像问题是调用的顺序,当你设置的窗口。在分配rootViewController之前,您需要拨打makeKeyAndVisible

self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; 
[self.window makeKeyAndVisible]; 
self.window.rootViewController = self.YourViewController; 
相关问题