2

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.

This is my screen

enter image description here

This is my code

#import "CameraViewController.h"
#import "WelcomeViewController.h"
#import <AVFoundation/AVFoundation.h>
#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 < 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

Edited

I forgot to put this piece of code in the above code. This one is also in my 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:&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];


}
Hanushka Suren
  • 723
  • 3
  • 10
  • 32
  • Are you getting an error ? or you are asking how to approach the problem? – iShaalan Jun 29 '15 at 11:40
  • You said "i can't add two camera views at once. I could put only one." That's not informative. Why not? What happens? IB refuses to add them? You get an error at runtime? The views are both on-screen but only one of them displays the camera feed? – Duncan C Jun 29 '15 at 11:44
  • When I do the same thing to the second page that means to the second view it shows a white screen but nothing. That's why I said I cant add two at once. Yes you are correct, The views are both on-screen but only one of them displays the camera feed and i get no errors when running the application. – Hanushka Suren Jun 29 '15 at 11:48
  • @Boyka In that case, take a look here: http://stackoverflow.com/questions/16543075/avcapturesession-with-multiple-previews – Daniel Jun 29 '15 at 12:07
  • @simpleBob I already saw this and tried. but I couldn't apply it into my scenario since my one is having pages. Anyway i will give it a try again – Hanushka Suren Jun 29 '15 at 12:15

0 Answers0