Alv*_*var 0 avfoundation avcapturesession swift
假设我想从相机输出中存储一帧
let imageBuffer:CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
some_list.append(imageBuffer.copy())
Run Code Online (Sandbox Code Playgroud)
**
这是通过扩展到 CVPixelBuffer 定义复制函数的方式
extension CVPixelBuffer {
func copy() -> CVPixelBuffer {
precondition(CFGetTypeID(self) == CVPixelBufferGetTypeID(), "copy() cannot be called on a non-CVPixelBuffer")
var _copy : CVPixelBuffer?
CVPixelBufferCreate(
nil,
CVPixelBufferGetWidth(self),
CVPixelBufferGetHeight(self),
CVPixelBufferGetPixelFormatType(self),
CVBufferGetAttachments(self, CVAttachmentMode.shouldPropagate),
&_copy)
guard let copy = _copy else { fatalError() }
CVPixelBufferLockBaseAddress(self, CVPixelBufferLockFlags.readOnly)
CVPixelBufferLockBaseAddress(copy, CVPixelBufferLockFlags(rawValue: 0))
let dest = CVPixelBufferGetBaseAddress(copy)
let source = CVPixelBufferGetBaseAddress(self)
let height = CVPixelBufferGetHeight(self)
let bytesPerRow = CVPixelBufferGetBytesPerRow(self)
memcpy(dest, source, height * bytesPerRow)
CVPixelBufferUnlockBaseAddress(copy, CVPixelBufferLockFlags(rawValue: 0))
CVPixelBufferUnlockBaseAddress(self, CVPixelBufferLockFlags.readOnly)
return copy
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,我是否需要明确管理我创建的 CVPixelBuffer 副本?还是 swift 通过引用计数来处理它?
Swift 管理您的缓冲区对象,因此您不必考虑释放它。
从带注释的 API 返回的 Core Foundation 对象在 Swift 中自动进行内存管理——您不需要自己调用 CFRetain、CFRelease 或 CFAutorelease 函数。
事实上,没有 Swift 版本的CVPixelBufferRelease
函数。
https://developer.apple.com/documentation/corevideo/1563589-cvpixelbufferrelease
归档时间: |
|
查看次数: |
715 次 |
最近记录: |