0

Recently I'm trying to extract depth data and portrait effects matte data using portrait images. I tried to do that with portrait images (.HEIC format) taken by iPhone 11 with iOS 14.3. But I was unable to extract depth data and matte data from that images.

Here is the code snippet which I used to extract matte data from .HEIC format portrait image,

func portraitEffectsMatteImageAt(_ path: String) -> UIImage? { let bundlePath = Bundle.main.bundlePath

// Check that the image at given path contains auxiliary PEM data:
guard let fileURL = NSURL(fileURLWithPath: bundlePath).appendingPathComponent(path),
let source = CGImageSourceCreateWithURL(fileURL as CFURL, nil),
let auxiliaryInfoDict = CGImageSourceCopyAuxiliaryDataInfoAtIndex(source, 0, kCGImageAuxiliaryDataTypePortraitEffectsMatte) as? [AnyHashable: Any],
let matteData = try? AVPortraitEffectsMatte(fromDictionaryRepresentation: auxiliaryInfoDict),
let matteCIImage = CIImage(portaitEffectsMatte: matteData)
else {
    return nil
}
return UIImage(ciImage: matteCIImage)

}

That function returns nil instead of returning matte image. So how can I extract matte data and depth data from .HEIC format images?

Thanks a lot

VidU
  • 21
  • 3
  • Maybe it would be more useful to do try rather than try? and then catch and print any eventual errors – Joakim Danielson Mar 26 '21 at 11:11
  • I would confirm whether the image has a depth channel. Not all cameras and not all devices are capable of capturing the depth channel. See https://developer.apple.com/documentation/avfoundation/avcapturedevice/devicetype/2933376-builtintruedepthcamera# – Idr Apr 06 '21 at 23:43

0 Answers0