2

Now, I can capture an image using avfoundation , like below. But how should I do to capture images (e.g. 20 or 30 images) continuously?

[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
 {
     CFDictionaryRef exifAttachments = CMGetAttachment( imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
     if (exifAttachments)
     {
         // Do something with the attachments.
         NSLog(@"attachements: %@", exifAttachments);
     }
     else
         NSLog(@"no attachments");

     NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
     UIImage *image = [[UIImage alloc] initWithData:imageData];

     // use the image

 }];
Fei Yang
  • 195
  • 3
  • 11

1 Answers1

2

Whoops, I forgot to follow up on your comment:

The scenario you describe would most easily be solved by using some sort of timer.
Depending on what level of accuracy you need, there are different candidates to look for:

  1. A repeating NSTimer is pretty easy and straight-forward to use. As this class works in conjunction with runloops, there are some pitfalls to be aware of — one being, that the accuracy is limited (but for what you seemingly want to accomplish, that should not be a problem at all).
  2. If you need a little more accuracy in the long run, you can still use NSTimer: Use initWithFireDate:… with repeats:NO and create a new timer this way, (using a date relative to the supposed fireDate of the old one) when the old one fires.
  3. If you really need a high degree of accuracy, you should have a look into dispatch timers. They are a part of GCD and, thus, a pretty low-level tech. If you decide to follow this path for accuracy, you should probably use your own dispatch queue in the call to dispatch_source_create.

In any case, your code for snapping the picture goes into the respective handler.

danyowdee
  • 4,658
  • 2
  • 20
  • 35
  • Thanks! I use NSTimer to capture images. But it can not capture too quickly.It gave some error like this "imageSampleBuffer in NULL"... – Fei Yang Aug 17 '11 at 16:34
  • That's sounds odd. Can you provide more detail? What is the exact error passed into your completion-handler when `imageSampleBuffer` is `NULL`? And: does that still occurr when you are running on the device whithout the debugger attached to your program? – danyowdee Aug 19 '11 at 05:11
  • I have a similar problem. Firing off multiple captureStillImageAsynchronouslyFromConnection calls, even within 5 seconds of each other, seems to prevent the previous call from completing until all images are captured, but subsequent calls fail (perhaps the first buffer gets overwritten). Would be very interested to see some working code to solve this problem. – Meekohi Jan 03 '12 at 16:26
  • @Meekohi Did you find any solution to the above problem. I also want to capture images in "Burst" Mode. But the problem that I suspect is previous buffer gets overwritten with new image data. Are we approaching the hardware limits of the device? – shshnk Feb 17 '14 at 10:02
  • @shshnk No, we ended up using video capture instead and reading frames as needed which seems to work well. I still consider this a bug on Apple's side since the second callback never returns. – Meekohi Feb 17 '14 at 13:58