以下代码似乎无法从图像中提取深度数据(包含深度信息)。代码在" guard {auxdepthinfo -...)"之后返回nil。线。在以下代码中,图像是对存储在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
}
}