在 iPhone 中使用 AVFoudation 连续捕获图像
现在,我可以使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
哎呀,我忘了跟进您的评论:
您描述的场景最容易通过使用某种计时器来解决。
根据您需要的准确度级别,需要寻找不同的候选者:
initWithFireDate:...
和repeats:NO
并以这种方式创建一个新的计时器,(使用当旧的触发时,相对于旧的假定的 fireDate 的日期)。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:
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).initWithFireDate:…
withrepeats: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.dispatch_source_create
.In any case, your code for snapping the picture goes into the respective handler.