Search code examples
iosobjective-cavcapturesessionavcapture

How can I ensure that my AVCaptureSession has enough time to run?


I need to ensure that my AVCaptureSession runs for 15 seconds before being stopped. How could I go about doing this? I'm hesitant about using wait or sleep because I'm fairly sure that it would halt the intake of input. What can I use to wait 15 seconds before I run the stop command?

Sample code:

[self.session startRunning];
// wait 15 seconds 
[self.session stopRunning]; 

Solution

  • Sleep would indeed bring everything to a screeching halt. Don't do that.

    You could use a call to dispatch_after:

    [self.session startRunning];
    // wait 15 seconds 
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 
      15 * NSEC_PER_SEC), 
      dispatch_get_main_queue(), ^{
        [self.session stopRunning]; 
    });
    

    Another alternative would be to set up a CADisplayLink and test to see if 15 seconds has elapsed on each call, but the above should be fine unless you need millisecond accuracy.