2016-04-11 5 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]; 
} 

カメラは私のUIViewControllerのプロパティですUIViewのあるコードは以下のとおりである

、再び

-(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

私は編集した答えでコードを追加しました。チェックしてください –

答えて

0

らしいです。 rootViewControllerを割り当てる前にmakeKeyAndVisibleに電話する必要があります。

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