一:CMSampleBufferRef浅拷贝
通过AVCaptureVideoDataOutput
获取到的CMSampleBufferRef
是放在缓冲区中的,会自动释放,不需要主动释放。
- (void)captureOutput:(AVCaptureOutput *)output didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
}
一般情况下我们会对获取的CMSampleBufferRef
做操作,此时可能会想先copy一份CMSampleBufferRef
,调用:
CMSampleBufferRef tempBufferRef;
OSStatus status = CMSampleBufferCreateCopy(kCFAllocatorDefault, nv12Buffer, &tempBufferRef);
if (noErr == status) {
CVImageBufferRef tempPixelBuffer = CMSampleBufferGetImageBuffer(tempBufferRef);
return tempPixelBuffer;
}
但是CMSampleBufferCreateCopy
是浅拷贝,如果拷贝出来的对象没有及时释放,会导致缓冲区占满,图像就卡住了:
如果多个样本缓冲区长时间引用此类内存池,则输入将无法再将新样本复制到内存中,并且这些样本将被丢弃。如果您的应用程序通过保留提供的CMSampleBuffer对象太久而导致删除样本,但需要长时间访问样本数据,请考虑将数据复制到新缓冲区中,然后在样本缓冲区上调用CFRelease (如果以前保留过),以便可以重用它引用的内存。
解决方案:
1.不拷贝,直接操作CMSampleBufferRef
。
1.使用CMSampleBufferRef
的深拷贝(不建议使用)。
浅拷贝使用示例:
http://www.voidcn.com/article/p-kpbktkmi-byq.html
二:CMSampleBufferRef深拷贝:
https://blog.csdn.net/weixin_30278311/article/details/95405935
网友评论