How to initialize CVPixelBufferRef in Swift

We used to initialize CVPixelBufferRef, as shown below.

  CVPixelBufferRef pxbuffer = NULL;

But in Swift we cannot use NULL, so we tried to follow, but, of course, XCODE wants it to be activated in order to use it

let pxbuffer: CVPixelBufferRef

but how?

In Obj_C, we created such a buffer, but, as I tried to explain above when converting to Swift, I was stopped on the first line.

 CVPixelBufferRef pxbuffer = NULL;

CVPixelBufferCreate(kCFAllocatorDefault, picGenislik,
                    frameHeight, kCVPixelFormatType_32ARGB, (__bridge         CFDictionaryRef) options,
                    &pxbuffer);
+4
source share
3 answers

Use CVPixelBufferCreate(_:_:_:_:_:_:)to create an object.

Adding demo code, hope this helps you. Also read Using Legacy C API 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)
+7
source

(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

EDIT:

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

, , , , :

var pixelBuffer : CVPixelBuffer? = nil
CVPixelBufferCreate(kCFAllocatorDefault, cgimg.width, cgimg.height, kCVPixelFormatType_32BGRA, nil, &pixelBuffer)
+2
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
}
0
source

Source: https://habr.com/ru/post/1611083/


All Articles