使用avfoundation捕捉图像,但捕捉速度不能太快

发布于 2024-11-28 19:42:22 字数 915 浏览 1 评论 0原文

我使用avfoundation来捕获图像,但是我不能捕获太快(我将间隔时间设置为0.1s)。它说“空样本缓冲区”。问题是什么?谢谢。

[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:] - NULL 示例缓冲区。”

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱殇璃 2024-12-05 19:42:22

嗯,它在文档中说:

如果 jpegSampleBuffer 为 NULL 或不是 JPEG 格式,此方法将引发 NSInvalidArgumentException。

因此,无论您检查 imageSampleBuffer 是否为 NULL,还是我所做的,我将整个事情包装在 if 语句检查中:CMSampleBufferIsValid(imageSampleBuffer)但真的不知道这是否是正确的调用。文档有点稀疏。

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.

°如果伤别离去 2024-12-05 19:42:22

现在对我有帮助的是

[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 。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文