How to crop and transform AVDepthData

Hello, now I have a trouble dealing with AVDepthData. What I want to do is crop and flip AVDepthData and replace original depthdata like below.

let newDepthBuffer: CVPixelBuffer
let originalDepthData: AVDepthData

// Create new AVDepthData object from rendered buffer
let outputDepthData = try originalDepthData.replacingDepthDataMap(with: newDepthBuffer)


I tried it like below

func photoOuput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {

     var finalDepthData: AVDepthData?
     if let depthData = photo.depthData {
          let ciDepthData = CIImage(cvImageBuffer: depthData.depthDataMap)
          let cgDepthData = CIContext().createCGImage(ciDepthData, from: ciDepthData.extent)
          let croppedCGDepthData = cgDepthData?.cropping(to: newRect)!
                        
          let newWidth = croppedCGDepthData!.width
          let newHeight = croppedCGDepthData!.height
                                                
          var destPixelBuffer: CVPixelBuffer?
          let attrs = [kCVPixelBufferCGImageCompatibilityKey: true, kCVPixelBufferCGBitmapContextCompatibilityKey: true]
          CVPixelBufferCreate(kCFAllocatorDefault, newWidth, newHeight, CVPixelBufferGetPixelFormatType(depthData.depthDataMap), attrs as CFDictionary, &destPixelBuffer)
                        
          CVPixelBufferLockBaseAddress(destPixelBuffer!, CVPixelBufferLockFlags(rawValue: 0))
                        
          let context = CGContext(data: CVPixelBufferGetBaseAddress(destPixelBuffer!), width: newWidth, 
                                   height: newHeight, bitsPerComponent: 8,     
                                   bytesPerRow: CVPixelBufferGetBytesPerRow(destPixelBuffer!), 
                                   space: CGColorSpaceCreateDeviceGray(), bitmapInfo: CGImageAlphaInfo.none.rawValue)
                        
          context?.draw(cgDepthData!, in: CGRect(x: 0, y: 0, width: newWidth, height: newHeight))
          context?.translateBy(x: 1, y: -1)
                        
          finalDepthData = try! photo.depthData?.replacingDepthDataMap(with: destPixelBuffer!)
                        
          CVPixelBufferUnlockBaseAddress(destPixelBuffer!, CVPixelBufferLockFlags(rawValue: 0))
      }
}


but it didn't work. It gave me just a black image.

I also tried below.

let cgDepthData = CIContext().createCGImage(ciDepthData, from: ciDepthData.extent, 
                                              format: CIFormat.L16, colorSpace: ciDepthData.colorSpace)

I tried lots of CIFormats but none of them worked.


Also, I tried lots of colorSpace options and bitmapInfo options when creating CGContext.


I can't figure out how to deal with AVDepthData.

How to crop and transform AVDepthData
 
 
Q