fetchAssetCollectionsWithType 仅检索视频

问题描述 投票:0回答:3

我正在努力在 iOS 中制作自定义图库视图 为此,我正在使用到目前为止效果很好的照片框架 我无法让它工作的一件事是获取结果只返回视频

我试过了

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

感谢您的帮助

ios swift ios8 photosframework
3个回答
4
投票

好的,我找到了问题的答案 基本上我需要使用一个谓词,这基本上是说检索媒体类型为视频的资产,并按创建日期对其进行排序

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

0
投票

这里是 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;

0
投票

以下是如何使用 Swift 5.x 做到这一点:

let options = PHFetchOptions()
options.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
let fetchResult = PHAsset.fetchAssets(with: .video, options: options)
© www.soinside.com 2019 - 2024. All rights reserved.