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
}
}