Advertise here

Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Using the camera to grab frames

suksmosuksmo Posts: 262Registered Users
edited October 2010 in iOS SDK Development

been playing around with AV Foundation. I've been trying to set up a camera using it and then grab some frames so I can manipulate them. I'm struggling. I've watched the WWDC videos on this and have looked at the sample code but I've still not got it working. I'd be grateful if someone could help me - I'm really trying to make something really worthwhile.

There's a technical note here - Technical Q&A QA1702: How to capture video frames from the camera as images using AV Foundation

my code looks like this:-

in the h file
#import <UIKit/UIKit.h>

#import <AVFoundation/AVFoundation.h>

#import <QuartzCore/QuartzCore.h>

#import <CoreMedia/CoreMedia.h>

@interface testerViewController : UIViewController {

     AVCaptureSession*            session;

    AVCaptureDevice*             device;

    NSError*                     error;

    AVCaptureInput*              input;

     AVCaptureStillImageOutput*   output;



in the m file
#import "testerViewController.h"

@implementation testerViewController

// Create and configure a capture session and start it running

- (void)setupCaptureSession 


    NSError *error = nil;

    // Create the session

    AVCaptureSession *session = [[AVCaptureSession alloc] init];

    // Configure the session to produce lower resolution video frames, if your 

    // processing algorithm can cope. We'll specify medium quality for the

    // chosen device.

    session.sessionPreset = AVCaptureSessionPresetMedium;

    // Find a suitable AVCaptureDevice

    AVCaptureDevice *device = [AVCaptureDevice


    // Create a device input with the device and add it to the session.

    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device 


    if (!input) {

        // Handling the error appropriately.


    [session addInput:input];

    // Create a VideoDataOutput and add it to the session

    AVCaptureVideoDataOutput *output = [[[AVCaptureVideoDataOutput alloc] init] autorelease];

    [session addOutput:output];

    // Configure your output.

    dispatch_queue_t queue = dispatch_queue_create("myQueue", NULL);

    [output setSampleBufferDelegate:self queue:queue];


    // Specify the pixel format

    output.videoSettings = 

    [NSDictionary dictionaryWithObject:

     [NSNumber numberWithInt:kCVPixelFormatType_32BGRA] 


    // If you wish to cap the frame rate to a known value, such as 15 fps, set 

    // minFrameDuration.

    output.minFrameDuration = CMTimeMake(1, 15);

    // Start the session running to start the flow of data

    [session startRunning];

    // Assign session to an ivar.

    [self setSession:session];


// Delegate routine that is called when a sample buffer was written

- (void)captureOutput:(AVCaptureOutput *)captureOutput 


       fromConnection:(AVCaptureConnection *)connection


    // Create a UIImage from the sample buffer data

    UIImage *image = [self imageFromSampleBuffer:sampleBuffer];


// Create a UIImage from sample buffer data

- (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer 


    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);

    // Lock the base address of the pixel buffer


    // Get the number of bytes per row for the pixel buffer

    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer); 

    // Get the pixel buffer width and height

    size_t width = CVPixelBufferGetWidth(imageBuffer); 

    size_t height = CVPixelBufferGetHeight(imageBuffer); 

    // Create a device-dependent RGB color space

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 

    if (!colorSpace) 


        NSLog(@"CGColorSpaceCreateDeviceRGB failure");

        return nil;


    // Get the base address of the pixel buffer

    void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer);

    // Get the data size for contiguous planes of the pixel buffer.

    size_t bufferSize = CVPixelBufferGetDataSize(imageBuffer); 

    // Create a Quartz direct-access data provider that uses data we supply

    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, baseAddress, bufferSize, 


    // Create a bitmap image from data supplied by our data provider

    CGImageRef cgImage = 







                  kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Little,







    // Create and return an image object representing the specified Quartz image

    UIImage *image = [UIImage imageWithCGImage:cgImage];


    CVPixelBufferUnlockBaseAddress(imageBuffer, 0);

    return image;}


// The designated initializer. Override to perform setup that is required before the view is loaded.

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {

    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {

        // Custom initialization


    return self;




// Implement loadView to create a view hierarchy programmatically, without using a nib.

- (void)loadView {




// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.

- (void)viewDidLoad {

    [super viewDidLoad];




// Override to allow orientations other than the default portrait orientation.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    // Return YES for supported orientations

    return (interfaceOrientation == UIInterfaceOrientationPortrait);



- (void)didReceiveMemoryWarning {

     // Releases the view if it doesn't have a superview.

    [super didReceiveMemoryWarning];


     // Release any cached data, images, etc that aren't in use.


- (void)viewDidUnload {

     // Release any retained subviews of the main view.

     // e.g. self.myOutlet = nil;


- (void)dealloc {

    [super dealloc];



This is throwing up CVPixel errors. I'd be grateful if someone could help put me in the right direction.
Post edited by suksmo on
[SIGPIC][/SIGPIC] FREE live action video puzzler<br />
<br />
download it FREE now:-<br />
<a href=""; target="_blank"></a>;


  • jjgalvezjjgalvez Posts: 1New Users
    edited August 2010
    It seems you are getting the same errors I got when I followed that tutorial. You may be missing CoreVideo and CoreMedia frameworks, that did the trick for me. Good luck!
  • dhilip10000dhilip10000 Posts: 16Registered Users
    edited October 2010
    jjgalvez wrote: »
    It seems you are getting the same errors I got when I followed that tutorial. You may be missing CoreVideo and CoreMedia frameworks, that did the trick for me. Good luck!

    hi your solution was helpful to me.. It cleared me all the errors.. but the tutorial refuses to run in my iphone.. [ the app does not launch itself.. i had no errors!]
Sign In or Register to comment.