如何快速将 a 转换CGImage
为 a CVPixelBuffer
?
我知道一些试图做相反的问题,以及一些客观的 C 答案,比如这个,但我无法让它们迅速工作。这是我最接近的:
func pixelBufferFromCGImage(image: CGImage) -> CVPixelBuffer {
var pxbuffer: CVPixelBuffer? = nil
let options: NSDictionary = [:]
let width = image.width
let height = image.height
let bytesPerRow = image.bytesPerRow
let dataFromImageDataProvider = image.dataProvider!.data
let x = CFDataGetBytePtr(dataFromImageDataProvider)
CVPixelBufferCreateWithBytes(
kCFAllocatorDefault,
width,
height,
kCVPixelFormatType_32ARGB,
CFDataGetBytePtr(dataFromImageDataProvider),
bytesPerRow,
nil,
nil,
options,
&pxbuffer
)
return pxbuffer!;
}
(这不会编译,因为CVPixelBufferCreateWithBytes
除了 UnsafeMutablePointer 并且CFDataGetBytePtr(dataFromImageDataProvider)
是UnsafePointer<UIint8>!
)