在 iPhone 中使用 AVFoudation 连续捕获图像

发布于 2024-11-28 01:45:09 字数 749 浏览 2 评论 0原文

现在,我可以使用 avfoundation 捕获图像,如下所示。但是我应该如何连续捕获图像(例如20或30张图像)?

[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

 }];

Now, I can capture an image using avfoundation , like below. But how should I do to capture images (e.g. 20 or 30 images) continuously?

[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

 }];

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

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

发布评论

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

评论(1

污味仙女 2024-12-05 01:45:09

哎呀,我忘了跟进您的评论:

您描述的场景最容易通过使用某种计时器来解决。
根据您需要的准确度级别,需要寻找不同的候选者:

  1. 重复的 NSTimer 使用起来非常简单直接。由于此类与 runloop 结合使用,因此需要注意一些陷阱 - 其中之一是准确性有限(但对于您看似想要完成的任务来说,这根本不应该是问题)。
  2. 如果从长远来看你需要更高的准确性,你仍然可以使用 NSTimer:使用 initWithFireDate:...repeats:NO 并以这种方式创建一个新的计时器,(使用当旧的触发时,相对于旧的假定的 fireDate 的日期)。
  3. 如果您确实需要高精度,您应该考虑一下调度计时器。它们是 GCD 的一部分,因此是一种相当低级的技术。如果您决定遵循此路径以确保准确性,则可能应该在对 dispatch_source_create 的调用中使用自己的调度队列。

无论如何,用于拍摄图片的代码都会进入相应的处理程序。

Whoops, I forgot to follow up on your comment:

The scenario you describe would most easily be solved by using some sort of timer.
Depending on what level of accuracy you need, there are different candidates to look for:

  1. A repeating NSTimer is pretty easy and straight-forward to use. As this class works in conjunction with runloops, there are some pitfalls to be aware of — one being, that the accuracy is limited (but for what you seemingly want to accomplish, that should not be a problem at all).
  2. If you need a little more accuracy in the long run, you can still use NSTimer: Use initWithFireDate:… with repeats:NO and create a new timer this way, (using a date relative to the supposed fireDate of the old one) when the old one fires.
  3. If you really need a high degree of accuracy, you should have a look into dispatch timers. They are a part of GCD and, thus, a pretty low-level tech. If you decide to follow this path for accuracy, you should probably use your own dispatch queue in the call to dispatch_source_create.

In any case, your code for snapping the picture goes into the respective handler.

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