我必须使用AVCaptureStillImageOutput captureStillImageAsynchronouslyFromConnection:
捕获一堆图像并使用 OpenGL 作为纹理在屏幕上渲染它们,我从传递 CVPixelBufferRef >捕获输出的CMSampleBufferRef 。第一次捕获成功发生,但对于后续捕获,我收到错误
Error Domain=AVFoundationErrorDomain Code=-11801 "Cannot Complete Action"
代码如下。
dispatch_async(dispatch_get_main_queue(), ^{
if (self.videoConnection) {
[self.imageOutput captureStillImageAsynchronouslyFromConnection:self.videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
if (!imageDataSampleBuffer) {
return ;
}
if (imageDataSampleBuffer) {
CVPixelBufferRef pxBuff = CMSampleBufferGetImageBuffer(imageDataSampleBuffer);
if ([self.captureDelegate respondsToSelector:@selector(didCaptureBufferRef:)]) {
[_captureDelegate didCaptureBufferRef:pxBuff];
}
}
}];
}});
和
didCaptureBufferRef:
最终调用此函数(我跳过了中间调用,因为 pxBuff 在任何地方都没有使用,只是传递过来)
- (void)textureFromBufferRef:(CVPixelBufferRef)pixelBuffer
{
size_t width = CVPixelBufferGetWidth(pixelBuffer);
size_t height = CVPixelBufferGetHeight(pixelBuffer);
glActiveTexture(GL_TEXTURE0);
CVReturn err = CVOpenGLESTextureCacheCreateTextureFromImage(kCFAllocatorDefault, _textureCacheRef, pixelBuffer, NULL, GL_TEXTURE_2D, GL_RED_EXT, (GLsizei)width, (GLsizei)height, GL_RED_EXT, GL_UNSIGNED_BYTE, 0, &_captureImageTextureRef);
glBindTexture(CVOpenGLESTextureGetTarget(_captureImageTextureRef), CVOpenGLESTextureGetName(_captureImageTextureRef));
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
我在这里做错了什么?当我删除
[_captureDelegate didCaptureBufferRef:pxBuff];
事情很好,捕获成功。