gpt4 book ai didi

swift - CGImage 到 CVPixelBuffer

转载 作者:搜寻专家 更新时间:2023-10-31 22:59:50 39 4
gpt4 key购买 nike

如何转换 CGImageCVPixelBuffer swift ?

我知道很多问题试图做相反的事情,还有一些客观的 C 答案,比如 this one但我无法让他们 swift 工作。这是我得到的最接近的:

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>! )

最佳答案

您需要使用 UnsafeMutablePointer,为此,您可以将 CFData 转换为 CFMutableData,然后获取 UnsafeMutablePointer 例如:

let dataFromImageDataProvider = CFDataCreateMutableCopy(kCFAllocatorDefault, 0, image.dataProvider!.data)
let x = CFDataGetMutableBytePtr(dataFromImageDataProvider)

关于swift - CGImage 到 CVPixelBuffer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38318387/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com