如何在Swift中初始化CVPixelBufferRef

时间:2015-10-10 11:21:08

标签: swift initialization

我们曾经如下初始化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);

3 个答案:

答案 0 :(得分:8)

使用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)

答案 1 :(得分:3)

这是另一种方法(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)

答案 2 :(得分:0)

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
}