通过提取资产集合排除iCloud共享相册

时间:2017-07-01 19:41:16

标签: ios phassetcollection phassetslibrary

我试图获取用户照片库中所有相册的列表,不包括任何iCloud共享相册。这是我到目前为止使用的代码:

PHFetchOptions *userAlbumsOptions = [PHFetchOptions new];
userAlbumsOptions.predicate = [NSPredicate predicateWithFormat:@"estimatedAssetCount > 0"];
userAlbumsOptions.includeAssetSourceTypes = PHAssetSourceTypeUserLibrary;

PHFetchResult *userAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:userAlbumsOptions];

for (PHAssetCollection *album in userAlbums) {
    [self.albums addObject:album];
}

如果我理解正确,userAlbumsOptions.includeAssetSourceTypes属性应该过滤掉iCloud共享相册;但是,它们仍然显示在列表中。任何人都可以提出我可能做错的建议吗?

1 个答案:

答案 0 :(得分:1)

一种可能的解决方案:

PHFetchOptions *userAlbumsOptions = [PHFetchOptions new];
userAlbumsOptions.predicate = [NSPredicate predicateWithFormat:@"estimatedAssetCount > 0"];

PHFetchResult *userAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:userAlbumsOptions];

for (PHAssetCollection *album in userAlbums) {
    if (album.assetCollectionSubtype != PHAssetCollectionSubtypeAlbumCloudShared) {
        [self.albums addObject:album];
    }
}

基本上,检索所有资产集合,但只有在子类型不是PHAssetCollectionSubtypeAlbumCloudShared时才将它们添加到相册数组中。这有效,但我对它不满意。 <{1}}检查不一定是必要的。