PHFetchResult仅适用于自定义相册

时间:2015-10-18 04:35:22

标签: ios uicollectionview phfetchoptions

我只是尝试将位于名为“MyFolder”的自定义文件夹中的照片访问到我的UICollectionView。我可以使用以下代码轻松地使用“所有照片和视频”,但我无法弄清楚如何过滤结果只包括我的文件夹名称:

PHFetchOptions *allPhotosOptions = [PHFetchOptions new];
allPhotosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];

PHFetchResult *videos = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeVideo options:allPhotosOptions];

[videos enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) {
      //add assets to array
}];

我尝试过使用它:

allPhotosOptions.predicate = [NSPredicate predicateWithFormat:@"title = %@", @"Coached"];

但它不起作用。有人可以帮我这个吗?

1 个答案:

答案 0 :(得分:2)

哇。经过约5个多小时的痛苦。这很简单。以下是有效的代码:

__block PHAssetCollection *collection;

// Find the album
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.predicate = [NSPredicate predicateWithFormat:@"title = %@", @"YOUR_CUSTOM_ALBUM_NAME"];
collection = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum
                                                      subtype:PHAssetCollectionSubtypeAny
                                                      options:fetchOptions].firstObject;

PHFetchResult *collectionResult = [PHAsset fetchAssetsInAssetCollection:collection options:nil];

[collectionResult enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) {


        //add assets to an array for later use in the uicollectionviewcell

    }];