【问题标题】:How to initialise CVPixelBufferRef in Swift如何在 Swift 中初始化 CVPixelBufferRef
【发布时间】:2016-01-08 07:06:49
【问题描述】:

我们曾经像下面这样初始化 CVPixelBufferRef。

  CVPixelBufferRef pxbuffer = NULL;

但是在 Swift 中我们不能使用 NULL,所以我们尝试了以下但当然 XCODE 希望我们将它初始化以使用它

let pxbuffer: CVPixelBufferRef

但是如何?

在 Obj_C 中,我们像这样创建缓冲区,但正如我在上面试图解释的那样,在转换为 Swift 时,我已经在第一行停止了。

 CVPixelBufferRef pxbuffer = NULL;

CVPixelBufferCreate(kCFAllocatorDefault, picGenislik,
                    frameHeight, kCVPixelFormatType_32ARGB, (__bridge         CFDictionaryRef) options,
                    &pxbuffer);

【问题讨论】:

  • 任何采用NULL / nil 的引用都是可选变量,请检查我的答案我已经创建了一个可选的pixelBuffer

标签: swift initialization


【解决方案1】:

使用CVPixelBufferCreate(_:_:_:_:_:_:)创建对象

添加一些演示代码,希望对您有所帮助。也请阅读Using Legacy C APIs with Swift

var keyCallBack: CFDictionaryKeyCallBacks
var valueCallBacks: CFDictionaryValueCallBacks

var empty: CFDictionaryRef = CFDictionaryCreate(kCFAllocatorDefault, nil, nil, 0, &keyCallBack, &valueCallBacks)

var attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
    1,
    &keyCallBack,
    &valueCallBacks);


var iOSurfacePropertiesKey = kCVPixelBufferIOSurfacePropertiesKey

withUnsafePointer(&iOSurfacePropertiesKey) { unsafePointer in
    CFDictionarySetValue(attributes, unsafePointer, empty)
}

var width = 10
var height = 12
var pixelBuffer: CVPixelBufferRef? = nil
var status: CVReturn = CVPixelBufferCreate(kCFAllocatorDefault, width, height, kCVPixelFormatType_32BGRA, attributes, &pixelBuffer)

【讨论】:

  • 我尝试了 CVPixelBufferCreate(kCFAllocatorDefault, width, height, kCVPixelFormatType_32ARGB, options, pxbuffer) 在 let pxbuffer: CVPixelBufferRef bu 那里没有运气。它不喜欢 pxbuffer。
  • 你的最新补充让我进步了一点,谢谢。但是,我想知道为什么要在内存中打开一个位置进行像素计算就需要这么多代码。我不喜欢阅读太多,这也许就是为什么。
【解决方案2】:

这是另一种方法(Swift 3):

var pixelBuffer: UnsafeMutablePointer<CVPixelBuffer?>!
if pixelBuffer == nil {
pixelBuffer = UnsafeMutablePointer<CVPixelBuffer?>.allocate(capacity: MemoryLayout<CVPixelBuffer?>.size)
}
CVPixelBufferCreate(kCFAllocatorDefault, width, height, kCVPixelFormatType_32BGRA, attributes, pixelBuffer)

然后你可以像这样访问缓冲区对象:

pixelBuffer.pointee

编辑:

    pixelBuffer = UnsafeMutablePointer<CVPixelBuffer?>.allocate(capacity: MemoryLayout<CVPixelBuffer?>.size)

如果您在完成后不手动释放指针,则会造成内存泄漏,以避免这种情况将您的代码更改为以下内容:

var pixelBuffer : CVPixelBuffer? = nil
CVPixelBufferCreate(kCFAllocatorDefault, cgimg.width, cgimg.height, kCVPixelFormatType_32BGRA, nil, &pixelBuffer)

【讨论】:

  • 您可能希望使用 pixelBuffer.deallocate(capacity: MemoryLayout.size) 释放像素缓冲区
  • @RocketGarden 你说的对,谢谢指点,我提供了更好的解决方案
【解决方案3】:
func getCVPixelBuffer(_ image: CGImage) -> CVPixelBuffer? {
    let imageWidth = Int(image.width)
    let imageHeight = Int(image.height)

    let attributes : [NSObject:AnyObject] = [
        kCVPixelBufferCGImageCompatibilityKey : true as AnyObject,
        kCVPixelBufferCGBitmapContextCompatibilityKey : true as AnyObject
    ]

    var pxbuffer: CVPixelBuffer? = nil
    CVPixelBufferCreate(kCFAllocatorDefault,
                        imageWidth,
                        imageHeight,
                        kCVPixelFormatType_32ARGB,
                        attributes as CFDictionary?,
                        &pxbuffer)

    if let _pxbuffer = pxbuffer {
        let flags = CVPixelBufferLockFlags(rawValue: 0)
        CVPixelBufferLockBaseAddress(_pxbuffer, flags)
        let pxdata = CVPixelBufferGetBaseAddress(_pxbuffer)

        let rgbColorSpace = CGColorSpaceCreateDeviceRGB();
        let context = CGContext(data: pxdata,
                                width: imageWidth,
                                height: imageHeight,
                                bitsPerComponent: 8,
                                bytesPerRow: CVPixelBufferGetBytesPerRow(_pxbuffer),
                                space: rgbColorSpace,
                                bitmapInfo: CGImageAlphaInfo.premultipliedFirst.rawValue)

        if let _context = context {
            _context.draw(image, in: CGRect.init(x: 0, y: 0, width: imageWidth, height: imageHeight))
        }
        else {
            CVPixelBufferUnlockBaseAddress(_pxbuffer, flags);
            return nil
        }

        CVPixelBufferUnlockBaseAddress(_pxbuffer, flags);
        return _pxbuffer;
    }

    return nil
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-16
    • 2014-12-21
    • 2021-08-02
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多