使用AVFoundation捕捉图像,但无法过快地捕捉。

3
我使用AVFoundation来捕获图像,但我无法太快地捕获图像(我将时间间隔设置为0.1秒)。它显示“NULL样本缓冲区”。问题出在哪里?谢谢。
[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
 }]; 

* 应用程序因未捕获的异常 'NSInvalidArgumentException' 而终止,原因是:'* +[AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:] - 空样本缓冲区。'


遇到了同样的问题,你能解决这个问题吗? - BaSha
2个回答

2

根据文档,它说:

如果jpegSampleBuffer为NULL或不符合JPEG格式,则此方法会抛出NSInvalidArgumentException。

因此,您可以检查imageSampleBuffer是否为NULL,或者像我一样将整个内容都包装在if语句中,并检查:CMSampleBufferIsValid(imageSampleBuffer),但是我不确定这是否是正确的调用。文档有点缺乏。


0

现在帮助我的东西是

[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;
    });

在这里,我使用一个辅助类来运行会话,并使用计时器获取图像帧。因此,首先我调用我的捕获图像函数,然后延迟0.3秒。我使计时器失效并停止AVCaptureSession


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接