开发者

Use avfoundation to capture image, but can not capture too quickly

开发者 https://www.devze.com 2023-03-27 22:12 出处:网络
I use avfoundation to capture images, but I can not capture too quickly(I set interval time to 0.1s). It says \" NULL sample buffer\". What is the problem? Thank you.

I use avfoundation to capture images, but I can not capture too quickly(I set interval time to 0.1s). It says " NULL sample buffer". What is the problem? Thank you.

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

     NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
     UIImage *image = [[UIImage alloc] initWithData:imageData];
    //use the image
 }]; 

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* +[AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:] - NULL sample buffer.'


Well it says in the documentation:

This method throws an NSInvalidArgumentException if jpegSampleBuffer is NULL or not in the JPEG format.

So either you check your imageSampleBuffer for NULL or what I did, I wrapped the entire thing in an if-statement checking: CMSampleBufferIsValid(imageSampleBuffer) but don't really know if that is the correct call. Documentation is a bit sparse.


The thing helped me right now is

[helper captureImage];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.3 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        [scheduledTimer invalidate];
        [helper stopRunningSession];


        imageView.image = helper.imageToReturn;
    });

Here I am using a helper class to run the session and a timer to get the frames of image. So at first i call my capture image function and after a delay of 0.3 seconds. i am invalidating the timer and stopping the AVCaptureSession.

0

精彩评论

暂无评论...
验证码 换一张
取 消