Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Swift中初始化CVPixelBufferRef_Swift_Initialization - Fatal编程技术网

如何在Swift中初始化CVPixelBufferRef

如何在Swift中初始化CVPixelBufferRef,swift,initialization,Swift,Initialization,我们曾经初始化CVPixelBufferRef,如下所示 CVPixelBufferRef pxbuffer = NULL; 但在Swift中,我们不能使用NULL,所以我们尝试了如下操作,但当然XCODE希望我们将其初始化以使用它 let pxbuffer: CVPixelBufferRef 但是怎么做呢 在Obj_C中,我们是这样创建缓冲区的,但正如我在上面试图解释的,当转换为Swift时,我在第一行被停止了 CVPixelBufferRef pxbuffer = NULL; C

我们曾经初始化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);
用于创建对象

添加一些演示代码,希望对您有所帮助。也要读书


下面是另一种方法(Swift 3):

编辑:

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

在让pxbuffer:CVPixelBufferRef之后,我尝试了CVPixelBufferCreate(kcfalocatordefault,width,height,kCVPixelFormatType_32ARGB,options,pxbuffer),但没有运气。它不喜欢pxbuffer。您最近添加的内容让我进步了一点。谢谢。但是,我想知道为什么仅仅在内存中为像素计算打开一个位置就需要那么多代码。我不喜欢阅读太多,这就是为什么可能的原因。任何采用
NULL
/
nil
的引用都是可选变量,请检查我的答案。我已经创建了一个可选的
pixelBuffer
你可能想用pixelBuffer取消分配pixelBuffer。取消分配(容量:MemoryLayout.size)@RocketGarden你是对的,谢谢你指出这一点,我提供了一个更好的解决方案
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)
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
}