显示来自PHAsset的深度图

时间:2018-09-26 01:01:04

标签: ios swift depth phasset

我正在尝试读取深度图并将其覆盖在PHAsset的UIImageView上。

到目前为止,我有这个:

PHImageManager.default().requestImageData(for: self.asset!, options: nil, resultHandler:{ (data, responseString, orientation, info) -> Void in
    if let imageData: Data = data {
        if let imageSource = CGImageSourceCreateWithData(imageData as CFData, nil) {

            let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil)! as NSDictionary

            let auxDataType = kCGImageAuxiliaryDataTypeDisparity
            let auxDataInfo = CGImageSourceCopyAuxiliaryDataInfoAtIndex(imageSource, 0, auxDataType)
            if auxDataInfo == nil {
                print ("NO DEPTH")
            } else {
                do {
                    var depthData = try AVDepthData(fromDictionaryRepresentation: auxDataInfo as! [AnyHashable : Any])
                    if depthData.depthDataType != kCVPixelFormatType_DisparityFloat16 {
                        depthData = depthData.converting(toDepthDataType: kCVPixelFormatType_DisparityFloat16)
                    }
                    let depthMap : CVPixelBuffer = depthData.depthDataMap
                    var ciImage = CIImage(cvPixelBuffer: depthMap)
                    ciImage = ciImage.applyingFilter("CIDisparityToDepth", parameters: [:])
                    DispatchQueue.main.async {
                        self.imageView.image = UIImage(ciImage: ciImage)
                    }
                }
                catch { }

            }
        }

    }
})

问题:

  1. 深度图是不正确的,而且似乎含糊不清。
  2. 深度图的方向不正确
  3. 由于某种原因,深度图颜色为红色

0 个答案:

没有答案