使用照片框架仅获取实时照片不起作用

Fetch only Live Photos using Photos Framework dosen't work

我正在尝试从设备相册中仅获取实况照片。 我找到了这个 Objective C 代码,但由于某些原因,当将它转换为 Swift 时无法编译。

PHFetchOptions *options = [[PHFetchOptions alloc] init];
options.predicate = [NSPredicate predicateWithFormat: @"(mediaSubtype == %ld)", PHAssetMediaSubtypePhotoLive];
options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey: @"creationDate" ascending: NO]];
PHFetchResult<PHAsset *> *assetsFetchResults = [PHAsset fetchAssetsWithMediaType: PHAssetMediaTypeImage options: options];

Swift:

 fetchOptions.predicate = NSPredicate(format: "mediaSubtype == %ld", PHAssetMediaSubtype.PhotoLive)

抛出编译错误:

 Argument type 'PHAssetMediaSubtype' does not conform to expected type 'CVarArgType'

Any suggestions?

您不能向该方法提供 Swift 枚举值 - 它需要一个 long(正如 %ld 格式字符串所暗示的那样),因此请使用

fetchOptions.predicate = NSPredicate(format: "mediaSubtype == %ld", PHAssetMediaSubtype.PhotoLive.rawValue)

访问枚举值的底层整数值