fetchAssetCollectionsWithType 仅检索视频

fetchAssetCollectionsWithType to only retrieve videos

我正致力于在 iOS 中制作自定义画廊视图 为此,我正在使用迄今为止效果很好的照片框架 我无法让它工作的一件事是仅将结果获取到 return VIDEOS

我试过了 let result = PHAssetCollection.fetchAssetCollectionsWithType(.Moment, subtype: .SmartAlbumVideos, options: nil) 但它仍在检索图像 我想知道语法是否错误

感谢您的帮助

好的,我找到了问题的答案 基本上我需要使用一个谓词,这基本上是说检索媒体类型为视频的资产,并按 creationDate

对其进行排序
let allVideosOptions = PHFetchOptions()
allVideosOptions.predicate = NSPredicate(format: "mediaType == %d", PHAssetMediaType.Video.rawValue)
allVideosOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
videos = PHAsset.fetchAssetsWithOptions(allVideosOptions)

这里有 objective-c 个代码。

PHFetchOptions* fetchOptions;

// all photos
fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
if( self.bOnlyVideo == YES ) {
    fetchOptions.predicate = [NSPredicate predicateWithFormat:@"mediaType == %d", PHAssetMediaTypeVideo];
} else if( self.bOnlyPhoto == YES ) {
    fetchOptions.predicate = [NSPredicate predicateWithFormat:@"mediaType == %d", PHAssetMediaTypeImage];
}
self.allPhotos = [PHAsset fetchAssetsWithOptions:fetchOptions];
[fetchOptions release], fetchOptions = nil;