I have an image, created like this:
let image = CGImage(jpegDataProviderSource: dataProvider!,
decode: nil, shouldInterpolate: false,
intent: CGColorRenderingIntent.defaultIntent)
Now I want to convert it to CVPixelBuffer
.
I checked multiple solutions. It seems that they are out of date.
For example:
let image_cv = pixelBufferFromCGImage(image: <#T##CGImage#>)
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)
let dataFromImageDataProvider = CFDataCreateMutableCopy(kCFAllocatorDefault, 0, image.dataProvider!.data)
let x = CFDataGetMutableBytePtr(dataFromImageDataProvider)
CVPixelBufferCreateWithBytes(
kCFAllocatorDefault,
width,
height,
kCVPixelFormatType_32ARGB,
CFDataGetBytePtr(dataFromImageDataProvider),
bytesPerRow,
nil,
nil,
options,
&pxbuffer
)
return pxbuffer!;
}
Doesn't compile because:
Cannot convert value of type 'UnsafePointer<UInt8>?' to expected argument type 'UnsafeMutableRawPointer'
I also tried to use this function: https://github.com/hollance/CoreMLHelpers/blob/master/CoreMLHelpers/CGImage%2BCVPixelBuffer.swift
But I receive:
Value of optional type 'CVPixelBuffer?' (aka 'Optional<CVBuffer>') must be unwrapped to a value of type 'CVPixelBuffer' (aka 'CVBuffer')
How can I solve it?
Thanks
Can you try just replacing argument CFDataGetBytePtr(dataFromImageDataProvider) with the variable x?
So it looks like: