I'm trying to follow some code provided by Apple to retrieve an image mask from portrait mode photos using some new classes and objects introduced in iOS 12. The code is here:
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)
}
My only change is basically modifying the fileURL to use a jpg in my bundle. :
guard let fileURL = Bundle.main.url(forResource: "custom00", withExtension: "jpg")
However, stepping through the code allows me to see that the assignment of auxiliaryInfoDict
is nil
. I imported these JPGs from a previous project that utilized older techniques to create depth masks (https://www.raywenderlich.com/314-image-depth-maps-tutorial-for-ios-getting-started) so the jpg files should be fine.
Does anyone have a working sample project? Thanks