ilaunchpad 0 Newbie Poster

I'm trying to make a camera app using AVCaptureSession. For now I just want to see if the video input works or not. But it looks like there is no input and I can't seem to understand why.

- (void)viewDidLoad
{
    [super viewDidLoad];

    session = [[AVCaptureSession alloc] init];

    [self addVideoPreviewLayer];

    CGRect layerRect = [[[self view] layer] bounds];

    [[self  previewLayer] setBounds:layerRect];
    [[self  previewLayer] setPosition:CGPointMake(CGRectGetMidX(layerRect),
                                                                  CGRectGetMidY(layerRect))];
    [[[self view] layer] addSublayer:[self previewLayer]];


    UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    myButton.frame = CGRectMake(80, 320, 200, 44);
    [myButton setTitle : @"Click Me!" forState:UIControlStateNormal];
    [myButton addTarget:self action: @selector(scanButtonPressed) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:myButton];


}


-(void)addVideoPreviewLayer{

    [self setPreviewLayer:[[[AVCaptureVideoPreviewLayer alloc] initWithSession:[self session]] autorelease]];
    [[self previewLayer] setVideoGravity:AVLayerVideoGravityResizeAspectFill];

}

-(void) addVideoInput{

    AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];   
    if (videoDevice) {
        NSError *error;
        AVCaptureDeviceInput *videoIn = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
        if (!error) {
            if ([[self session] canAddInput:videoIn])
                [[self session] addInput:videoIn];
            else
                NSLog(@"Couldn't add video input");     
        }
        else
            NSLog(@"Couldn't create video input");
    }
    else
        NSLog(@"Couldn't create video capture device");
}

-(IBAction)scanButtonPressed{

    [self addVideoInput];
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.