【发布时间】:2018-10-15 22:25:40
【问题描述】:
我正在尝试按照 Apple 提供的一些代码,使用 iOS 12 中引入的一些新类和对象从肖像模式照片中检索图像蒙版。代码在这里:
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)
}
我唯一的改变基本上是修改 fileURL 以在我的包中使用 jpg。 :
guard let fileURL = Bundle.main.url(forResource: "custom00", withExtension: "jpg")
但是,单步执行代码让我看到auxiliaryInfoDict 的分配是nil。我从以前的项目中导入了这些 JPG,该项目利用旧技术创建深度蒙版 (https://www.raywenderlich.com/314-image-depth-maps-tutorial-for-ios-getting-started),因此 jpg 文件应该没问题。
有人有工作示例项目吗?谢谢
【问题讨论】:
-
而不是使用 url 尝试使用照片框架来获取照片并使用它的数据,看看是否有效
标签: ios swift avfoundation ios12 avkit