Ashish Agarwal
Ashish Agarwal

Reputation: 14925

Videos with AVFoundation

I am trying to get video working with AVFoundation. At the moment I just have a code which is supposed to get the input from the camera and display it on the UIImage on the screen. However, I just get a white screen.

-(CIContext*)context{
    if(!_context){
        _context = [CIContext contextWithOptions:nil];
    }
    return _context;
}

-(void)captureOutput:(AVCaptureOutput *)captureOutput didDropSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection{
    CVPixelBufferRef pb = CMSampleBufferGetImageBuffer(sampleBuffer);
    CIImage *ciImage = [CIImage imageWithCVPixelBuffer:pb];

    CGImageRef ref = [self.context createCGImage:ciImage fromRect:ciImage.extent];
    self.imageView.image = [UIImage imageWithCGImage:ref scale:1.0 orientation:UIImageOrientationRight];
CGImageRelease(ref);
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
     self.session = [[AVCaptureSession alloc] init];

     //the resolution of the capturing session
     self.session.sessionPreset = AVCaptureSessionPreset352x288;

     self.videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
     self.videoInput = [AVCaptureDeviceInput deviceInputWithDevice:
                   self.videoDevice error:nil];

     self.frameOutput = [[AVCaptureVideoDataOutput alloc] init];

     self.frameOutput.videoSettings = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey];

     [self.session addInput:self.videoInput];
     [self.session addOutput:self.frameOutput];
     [self.frameOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];

     [self.session startRunning];

 }

Can someone please help ?

Upvotes: 0

Views: 542

Answers (1)

Brent Westmoreland
Brent Westmoreland

Reputation: 463

The delegate method you registered was:

-(void)captureOutput:(AVCaptureOutput *)captureOutput didDropSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection

The delegate method you should have registered was:

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection

Upvotes: 1

Related Questions