问题描述
let bv = malloc(width * height * 4)!
var db = vImage_Buffer(data: bv,height: vImagePixelCount(height),width: vImagePixelCount(width),rowBytes: width*2)
// vImageConvert_PlanarftoPlanar8(&sourceBuffer,&destBuffer,1.0,0.0,vImage_Flags(kvImageNoFlags))
vImageConvert_PlanarftoPlanar16F(&sourceBuffer,&db,vImage_Flags(kvImageNoFlags))
let bp = bv.assumingMemoryBound(to: UInt8.self)
let p = CGDataProvider(data: CFDataCreateWithBytesNocopy(kcfAllocatorDefault,bp,width * height,kcfAllocatorDefault))!
let cgImage = CGImage(width: width,height: height,bitsPerComponent: 5,bitsPerPixel: 16,bytesPerRow: width,space: CGColorSpace(name: CGColorSpace.linearSRGB)!,bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.noneskipFirst.rawValue),provider: p,decode: nil,shouldInterpolate: false,intent: .defaultIntent)!
let savePath = self.documentsPath.appendingPathComponent("camera")
let sURL = savePath.appendingPathComponent(String(format: "image.png")
if let imageDestination = CGImageDestinationCreateWithURL(smoothedSceneDepthURL as CFURL,kUTTypePNG,1,nil) {
CGImageDestinationAddImage(imageDestination,cgImage,nil)
CGImageDestinationFinalize(imageDestination)
}
Apple文档说,bitsPerComponent和bitsPerPixel的这些值对于iOS here是正确的
但是出现以下错误:
[UnkNown process name] CGImageCreate: invalid image bits/pixel or bytes/row.
我能够导出8位灰度图像,并且可以根据需要发布参数
解决方法
如果每个像素16位,则每行将有2*width
个字节:
bytesPerRow: 2 * width,