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

邮差的信 提交于 2019-12-12 12:28:36

问题


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.
         // 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.'


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/7024282/use-avfoundation-to-capture-image-but-can-not-capture-too-quickly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!