【发布时间】:2019-02-08 23:35:03
【问题描述】:
我正在以预览模式捕捉视频,并希望显示相机捕捉到的静止图像。
我目前将图像和捕获输出保存到接口中定义的 ivars 为:
UIImage *snapshot
AVCaptureStillImageOutput* stillImageOutput;
视频显示正常。但是,当我尝试捕获并显示静止图像时,什么都没有出现,事实上,调试器显示 stillImageOutput 和图像为零。我认为这可能是异步捕获的时间问题,我需要使用完成处理程序,但我对完成处理程序很弱。
在不占用 UI 的情况下立即显示静止图像的正确方法是什么:
捕获静止的代码:
- (void)takeSnapshot {
AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in stillImageOutput.connections) {
for (AVCaptureInputPort *port in [connection inputPorts]) {
if ([[port mediaType] isEqual:AVMediaTypeVideo]) {
videoConnection = connection;
break;
}
}
if (videoConnection) {
break;
}
}
[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection
completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
if (imageDataSampleBuffer != NULL) {
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
snapshot = [UIImage imageWithData:imageData];
}
}];
}
显示静止的代码。请注意缺少完成处理程序,这可能是个问题,但是,我不确定如何编写...
[self takeSnapshot];
self.imageView.image = snapshot;
【问题讨论】:
标签: ios objective-c avfoundation avcapture