将2个PHFetchResults组合在一起

时间:2016-03-02 00:58:09

标签: ios swift photokit

Swift和iOS开发的新手。有人能告诉我如何将两个调用的结果组合到PHAssetCollection.fetchAssetCollectionsWithType中吗?具体来说,我正试图在一个列表中获得包含标准专辑和智能专辑的单个列表。

由于

2 个答案:

答案 0 :(得分:3)

我不确定这是最佳做法还是其他什么,但今天我需要做这样的事情,以下情况似乎有效:

@property NSMutableArray<PHAssetCollection *> *collections;

...

/* Get albums of interest */
PHFetchResult *albums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype: ... options: ...];
/* Get smart albums of interest (in my case, the Camera Roll) */
PHFetchResult *cameraRoll = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options: ...];
/* Load albums into collections member */
for (NSUInteger i = 0; i < albums.count; ++i)
    [self.collections addObject:[albums objectAtIndex:i]];
/* Load Camera Roll into collections member */
[self.collections addObject:[cameraRoll firstObject]];

请记住在向其添加对象之前初始化集合成员。

答案 1 :(得分:0)

您可以使用fetchAssetsWithLocalIdentifiers:options:PHAsset的api。 首先得到一张专辑:

PHFetchResult *album = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype: ... options: ...];

循环通过它:

for (PHAsset *asset in album) {     
    [self.localIDList addObject:asset.localIdentifier];
}

将此选项用于您需要的每张专辑。最后......

PHFetchResult *fetchResult = [PHAsset fetchAssetsWithLocalIdentifiers:self.localIDList options:nil];