5

The following code does not seem be able to extract depth data from an image (which contains depth information). The code returns nil after the "guard { auxdepthinfo -...)" line. In the following code, the image is a reference to a portrait image stored in the photoAlbum.

struct DepthReader {
    var image: UIImage

    func depthDataMap() -> CVPixelBuffer? {
        //create data from UIImage
        guard let imageDat: Data = UIImageJPEGRepresentation(image, 1.0) else {
            return nil
        }

        //create source
        guard let source = CGImageSourceCreateWithData(imageDat as CFData, nil) else {
            return nil
        }

        //extract auxData disparity data
        guard let auxDataInfo = CGImageSourceCopyAuxiliaryDataInfoAtIndex(source, 0, kCGImageAuxiliaryDataTypeDepth) as? [AnyHashable : Any] else {
            return nil
        }

        // This is the star of the show!
        var depthData: AVDepthData

        do {
            // Get the depth data from the auxiliary data info
            depthData = try AVDepthData(fromDictionaryRepresentation: auxDataInfo)
        } catch {
            return nil
        }

        // Make sure the depth data is the type we want
        if depthData.depthDataType != kCVPixelFormatType_DisparityFloat32 {
            depthData = depthData.converting(toDepthDataType: kCVPixelFormatType_DisparityFloat32)
        }
        return depthData.depthDataMap
    }
}
  • How far does this code get? – rmaddy Feb 18 '18 at 22:02
  • "The code returns nil" Which line? `depthData.depthDataMap`? Or one of your `guard let/else`, do/try/catch? If you don't specify which lines is causing the issue, how are we supposed to guess? – Larme Feb 18 '18 at 22:05
  • 1
    //extract auxData disparity data guard let auxDataInfo = CGImageSourceCopyAuxiliaryDataInfoAtIndex(source, 0, kCGImageAuxiliaryDataTypeDepth) as? [AnyHashable : Any] else { return nil } – Michael Archer Feb 18 '18 at 23:25

0 Answers0