title: Add two camera views at once in ios
body: <p>I am currently implementing an app in which there should be two camera views as two pages. i used a scrollview to implement paging thing and it was successful. The problem that i face now is i can't add two camera views at once. I could put only one. can someone help me.</p>
  
  <p>This is my screen</p>
  
  <p><img src="https://i.stack.imgur.com/o8SBe.png" alt="enter image description here"> </p>
  
  <p>This is my code</p>
  
  <pre><code>#import "CameraViewController.h"
  #import "WelcomeViewController.h"
  #import &lt;AVFoundation/AVFoundation.h&gt;
  #import "PhotoViewController.h"
  
  @interface CameraViewController ()
  
      @property (strong, nonatomic) UIWindow *window;
  
  @end
  
  @implementation CameraViewController{
  
      AVCaptureSession *session;
  
      AVCaptureStillImageOutput *stillImageOutput;
  
      UIView *view_Photo;
  
      UIView *view_Document;
  
      UIScrollView *cameraScroll;
  }
  
  
  int page = 0;
  
  float cameraScrollHeight;
  
  - (void)viewDidLoad {
  
      [super viewDidLoad];
  
      cameraScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(X, Y, Width,Height)];
  
      cameraScroll.pagingEnabled = YES;
  
      cameraScroll.bounces = NO;
  
      cameraScroll.showsHorizontalScrollIndicator = NO;
  
      cameraScroll.showsVerticalScrollIndicator = NO;
  
      cameraScroll.scrollsToTop = YES;
  
      NSInteger numberOfViews = 2;
  
      for (int i = 0; i &lt; numberOfViews; i++) {
  
          //set the origin of the sub view
  
          CGFloat myOrigin = i * self.view.frame.size.width;
  
          //create the sub view and allocate memory
  
          if (i == 0) {
  
              view_Photo = [[UIView alloc] initWithFrame:CGRectMake(myOrigin, 0, self.view.frame.size.width, ScrollViewHeight)];
  
              view_Photo.backgroundColor = [UIColor clearColor];        
  
          }else{
  
              view_Document = [[UIView alloc] initWithFrame:CGRectMake(myOrigin, 0, self.view.frame.size.width, ScrollViewHeight)];
  
              view_Document.backgroundColor = [UIColor clearColor];
  
          }
  
          //set the scroll view delegate to self so that we can listen for changes
          cameraScroll.delegate = self;
  
          //add the subview to the scroll view
  
          if (i == 0) {
  
              [cameraScroll addSubview:view_Photo];
  
          }else{
  
              [cameraScroll addSubview:view_Document];
  
          }
      }
  
      //set the content size of the scroll view, we keep the height same so it will only
  
      //scroll horizontally
  
  cameraScroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews,cameraScrollHeight);
  
      //we set the origin to the 3rd page
  
      CGPoint scrollPoint = CGPointMake(0, 0);
  
      //change the scroll view offset the the 3rd page so it will start from there
  
      [cameraScroll setContentOffset:scrollPoint animated:YES];
  
      [self.view addSubview:cameraScroll];
  
  }
  
  //scrolling ends
  - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
  
      //find the page number you are on
      CGFloat pageWidth = scrollView.frame.size.width;
      page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
      NSLog(@"Scrolling - You are now on page %i",page);
  }
  
  //dragging ends, please switch off paging to listen for this event
  - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView
                   withVelocity:(CGPoint)velocity
            targetContentOffset:(inout CGPoint *) targetContentOffset
  NS_AVAILABLE_IOS(5_0){
  
      //find the page number you are on
      CGFloat pageWidth = scrollView.frame.size.width;
      page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
      NSLog(@"Dragging - You are now on page %i",page);
  
  }
  
  - (void)didReceiveMemoryWarning {
      [super didReceiveMemoryWarning];
      // Dispose of any resources that can be recreated.
  }
  
  @end
  </code></pre>
  
  <p><b>Edited</b></p>
  
  <p>I forgot to put this piece of code in the above code. This one is also in my code.</p>
  
  <pre><code>- (void)viewWillAppear:(BOOL)animated{
  
       session = [[AVCaptureSession alloc] init];
  
       [session setSessionPreset:AVCaptureSessionPresetPhoto];
  
       AVCaptureDevice *inputDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  
       NSError *error;
  
       AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:inputDevice error:&amp;error];
  
       if ([session canAddInput:deviceInput]) {
  
           [session addInput:deviceInput];
       }
  
       AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
  
       [previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
  
       CALayer *rootLayer = [cameraScroll];
  
       [rootLayer setMasksToBounds:YES];
  
       CGRect frame = view_Photo;
  
       [previewLayer setFrame:frame];
  
       [rootLayer insertSublayer:previewLayer atIndex:0];
  
       stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
  
       NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG, AVVideoCodecKey,nil];
  
       [stillImageOutput setOutputSettings:outputSettings];
  
       [session addOutput:stillImageOutput];
  
       [session startRunning];
  
  
  }
  </code></pre>