【问题标题】:Do swift manage the memory of a CVPixelBuffer that I create from CVPixelBufferCreate?swift 管理我从 CVPixelBufferCreate 创建的 CVPixelBuffer 的内存吗?
【发布时间】:2019-06-12 07:14:12
【问题描述】:

假设我想存储来自相机输出的帧

let imageBuffer:CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
some_list.append(imageBuffer.copy())

**

这里是复制函数是如何通过扩展 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
    }
}

问题是,我需要显式管理我创建的 CVPixelBuffer 副本吗?还是 swift 通过引用计数来处理它?

【问题讨论】:

    标签: swift avfoundation avcapturesession


    【解决方案1】:

    Swift 管理您的缓冲区对象,因此您不必考虑释放它。

    从带注释的 API 返回的 Core Foundation 对象在 Swift 中自动进行内存管理——您无需自己调用 CFRetain、CFRelease 或 CFAutorelease 函数。

    https://developer.apple.com/documentation/swift/imported_c_and_objective-c_apis/working_with_core_foundation_types#2994152

    事实上,CVPixelBufferRelease 函数并没有 Swift 版本。

    https://developer.apple.com/documentation/corevideo/1563589-cvpixelbufferrelease

    【讨论】:

      猜你喜欢
      • 2016-02-18
      • 1970-01-01
      • 2018-06-08
      • 2015-03-13
      • 2012-08-26
      • 2010-09-13
      • 1970-01-01
      • 2016-10-19
      • 2023-03-27
      相关资源
      最近更新 更多