0
votes

I have a view controller in which I have added an UIView full screen size, In that UIView I have the AVCapturesession that helps me to capture photos, My view controller opens good in portrait mode but opens abruptly in landscape mode.

The code is as follows,

- (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];
}

The camera is the UIView which is the property of my UIViewController,

Again,

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

My view opens wrong in landscape mode once i rotate to portrait it gets fine and on again rotation to landscape it works good, only it does not launch properly in landscape mode why?

Here I am setting the root view controller,

 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];
1
can you add code where you setting rootViewController.Ronak Chaniyara
I have added the code in y edited answer. Please checkAvinash Sharma

1 Answers

0
votes

seems like problem is order of calls when you set up the window. You need to call makeKeyAndVisible before you assign the rootViewController.

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