ARkit depthmap is emty after convert depthmap to 16 bits

I am trying to build a point cloud with ARkit's depthmap, when I convert the depthmap to PNG file with following code, the depth map lose Z axis data.Any idea to solve this problem?

This is my code to store depthmap.

    let depthimage = frame.sceneDepth!.depthMap
     
    let ciImageDepth      = CIImage(cvPixelBuffer: depthimage)
    let contextDepth:CIContext = CIContext.init(options: nil)
    let cgImageDepth:CGImage  = context.createCGImage(ciImageDepth, from: ciImageDepth.extent)!
    let width:Int = cgImageDepth.width
    let height:Int = cgImageDepth.height
    let colorSpace:CGColorSpace = CGColorSpaceCreateDeviceGray()
    let bitmapInfo = CGBitmapInfo(rawValue: CGBitmapInfo.byteOrder16Little.rawValue)
    let depthcontext:CGContext = CGContext(data: nil, width: width, height: height, bitsPerComponent: 16, bytesPerRow: 0, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)!
     
    let rect:CGRect = CGRect.init(x: 0, y: 0, width: width, height: height)
      depthcontext.draw(cgImageDepth, in: rect)
    let outPutImage:CGImage = depthcontext.makeImage()!
    let newImage:UIImage = UIImage.init(cgImage: outPutImage, scale: 1, orientation: UIImage.Orientation.up)

I think what is most likely going on here is that CoreImage is clamping the depth values at 1, because it doesn't know that it needs to preserve values greater than 1 in this case.

If you want to produce a displayable image representation of the depthMap, you can normalize the values in the depthMap (i.e. find the largest depth value in the map (or choose some distant arbitrary threshold like 10 meters) and then divide every depth value by your threshold value).

ARkit depthmap is emty after convert depthmap to 16 bits
 
 
Q