AVcapture video preview not working.

Discussion in 'iOS Programming' started by THEDEAN911, Oct 23, 2012.

  1. THEDEAN911 macrumors newbie

    Feb 13, 2010
    I'm trying to get the camera video preview working for my app. Right now I get a black screen on the phone and a "Thread 1 breakpoint 1.2" on

    @property (weak, nonatomic) IBOutlet UIView *videoPreviewLayer;
    videoPreviewLayer is the view that the preview should show up on.

    This is my viewController.h

    #import <UIKit/UIKit.h>
    @class AVCaptureVideoPreviewLayer;
    @interface ViewController : UIViewController
    - (IBAction)imageCapture:(id)sender;
    @property (weak, nonatomic) IBOutlet UIView *videoPreviewLayer;
    This is my viewController.m

    #import "ViewController.h"
    #import <AVFoundation/AVFoundation.h>
    @interface ViewController ()
    @implementation ViewController
    - (void)viewDidLoad
        AVCaptureSession *session = [[AVCaptureSession alloc] init];
        session.sessionPreset = AVCaptureSessionPresetPhoto;
        AVCaptureDevice *device = [AVCaptureDevice
        AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:NULL];
        [session addInput:input];
    //    dispatch_queue_t queue = dispatch_queue_create("MyQueue", NULL);
    //    [output setSampleBufferDelegate:self queue:queue];
        AVCaptureVideoPreviewLayer *preview = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
        CALayer *viewLayer = [_videoPreviewLayer layer];
        [viewLayer setMasksToBounds:YES];
        CGRect bounds = [_videoPreviewLayer bounds];
        [preview setFrame:bounds];
    //    preview.frame = self.view.frame;
    //    [self.view.layer addSublayer:preview];
        [session startRunning];
        [super viewDidLoad];
    	// Do any additional setup after loading the view, typically from a nib.
    - (void)didReceiveMemoryWarning
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    - (IBAction)imageCapture:(id)sender {
    - (void)captureOutput:(AVCaptureOutput *) captureOutput
        didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
       // CFShow(sampleBuffer);
    Any ideas as to what's going on?

    Thanks in advance :)
  2. nickculbertson macrumors regular


    Nov 19, 2010
    Nashville, TN
    I'm not sure from you're code where the error occurs but I've been able to implement AVCaptureSessions using this post.


    It uses a singleton class rather than doing all the work in your main viewcontroller.

    Good Luck,

Share This Page