我试图在我的 iphone 中显示所有专辑标题的列表,当我按下标题时,我希望它在收藏视图中显示该特定专辑的所有照片。我正在使用“PhotosUI”框架,我使用此代码获取专辑标题:
-(void)getLibrariesList
{
NSArray *collectionsFetchResults;
albumNames = [[NSMutableArray alloc] init];
PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum
subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
PHFetchResult *syncedAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum
subtype:PHAssetCollectionSubtypeAlbumSyncedAlbum options:nil];
PHFetchResult *userCollections = [PHCollectionList fetchTopLevelUserCollectionsWithOptions:nil];
PHFetchOptions *userAlbumsOptions = [PHFetchOptions new];
userAlbumsOptions.predicate = [NSPredicate predicateWithFormat:@"estimatedAssetCount > 0"];
// Add each PHFetchResult to the array
collectionsFetchResults = @[smartAlbums, userCollections, syncedAlbums];
for (int i = 0; i < collectionsFetchResults.count; i ++) {
PHFetchResult *fetchResult = collectionsFetchResults[i];
for (int x = 0; x < fetchResult.count; x ++) {
PHCollection *collection = fetchResult[x];
albumNames[x] = collection.localizedTitle;
}
}
//update the tableview
[self.libraryTableView reloadData];
}
这段代码通过标题获取特定相册的所有照片:
-(void)showPhotosInCollectionViewForALbumWithName:(NSString*)albumName
{
libraryAssets = [NSMutableArray array];
__block PHAssetCollection *collection;
// Find the album
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.predicate = [NSPredicate predicateWithFormat:@"title = %@", albumName];
collection = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum
subtype:PHAssetCollectionSubtypeAny
options:fetchOptions].firstObject;
PHFetchResult *collectionResult = [PHAsset fetchAssetsInAssetCollection:collection options:nil];
[collectionResult enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) {
[libraryAssets addObject:asset];
}];
[self.libraryPhotosCollectoionView reloadData];
}
对于某些专辑,它正在工作,像“Instagram”这样的专辑和我用手机创建的专辑,但是当我按下“相机胶卷”和“我的照片流”专辑时,它什么也不显示。
任何想法为什么会发生这种情况,或者是否有其他方法可以获取我设备的所有照片?