2016-04-11 57 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; 
相關問題