在iOS的Photos框架中,为了向一组具有特定过滤器的PHAssets发出请求,您可以使用fetchAssetsWithOptions:options并传递一个带有筛选器的PHFetchOptions对象。
我试图过滤掉没有位置资产元数据对象的任何PHAssets,并且不完全确定是否可以在PHFetchOptions上使用predicate选项来完成。可能有另一种方法可以根据是否存在位置来筛选出资产,但我不完全确定是否有最有效的方法来做到这一点。
//Photos fetch
PHFetchOptions *options = [[PHFetchOptions alloc] init];
options = [NSPredicate predicateWithFormat:@"mediaType == %d", PHAssetMediaTypeImage];
options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
PHFetchResult *assetsFetchResults = [PHAsset fetchAssetsWithOptions:options];发布于 2015-10-14 14:17:15
根据Class/index.html的文档,这不能使用谓词来完成。PHAsset的位置属性不能在谓词/sortDescriptor中使用。
因此,唯一的选择是枚举PHFetchResult的对象,然后筛选出那些没有位置数据的对象。这当然比使用谓词慢,但可能仍然是一个解决方案,取决于您的用例。
使用此方法的示例:
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
PHFetchResult *result = [PHAsset fetchAssetsWithOptions:nil];
NSMutableArray *filteredAssets = [NSMutableArray new];
[result enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL * _Nonnull stop) {
if (asset.location != nil) {
[filteredAssets addObject:asset];
}
}];
//optional - create new Collection/fetchresult with filtered assets
PHAssetCollection *assetCollectionWithLocation = [PHAssetCollection transientAssetCollectionWithAssets:filteredAssets title:@"Assets with location data"];
PHFetchResult *filteredResult = [PHAsset fetchAssetsInAssetCollection:assetCollectionWithLocation options:nil];
}];https://stackoverflow.com/questions/33089745
复制相似问题