AVcapture video preview not working.

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

  1. THEDEAN911 macrumors newbie

    Joined:
    Feb 13, 2010
    #1
    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

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

    This is my viewController.h

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

    Code:
    #import "ViewController.h"
    #import <AVFoundation/AVFoundation.h>
    
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad
    {
        AVCaptureSession *session = [[AVCaptureSession alloc] init];
        session.sessionPreset = AVCaptureSessionPresetPhoto;
        AVCaptureDevice *device = [AVCaptureDevice
                                   defaultDeviceWithMediaType:AVMediaTypeVideo];
        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);
    }
    @end
    
    Any ideas as to what's going on?

    Thanks in advance :)
     
  2. nickculbertson macrumors regular

    nickculbertson

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

    http://www.musicalgeometry.com/?p=1273

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

    Good Luck,
    Nick
     

Share This Page