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

标签 ios swift nspredicate photosframework phasset

我正在尝试从设备相册中仅获取实时照片。 我找到了这个 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)

访问枚举值的底层整数值

关于ios - 使用照片框架仅获取实时照片不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35120774/

相关文章:

video - iOS:在播放过程中获取每一帧视频帧的内容

ios - 在 UIDocumentInteractionController 的 presentPreviewAnimated 中禁用操作项

ios - Swift 中创建子类对象的类函数

ios - 字典数组的 NSPredicate

iphone - 用于排除给定 NSManagedObject 的 NSPredicate 语法

ios - 需要为 IOS 应用启用服务器端访问

ios - 是否可以访问 xcode 中的所有通知

swift - 如何在自动 ID 子项中使用 queryEqualToValue 检索数据。 Firebase-iOS

ios - Swift - 切换 CollectionView 单元格以切换数组中的项目

iphone - NSPredicate 聚合函数 [SIZE] 给出 'unsupported function expression' 错误