我正在尝试在我的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“和我用手机创建的相册,但当我按下"Camera Roll”和"My Photo Stream“相册时,它不会显示任何内容。
你知道为什么会发生这种情况吗?或者有没有其他方法来获取我设备上的所有照片?
发布于 2016-09-19 15:28:03
我有答案了。我检索所有的相机滚动照片和视频。下面是我的代码。
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
collection = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum
subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary
options:fetchOptions].firstObject;
collectionResult = [PHAsset fetchAssetsInAssetCollection:collection options:nil];
NSLog(@"Custom album images::%@",collectionResult);
[collectionResult enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop)
{
NSLog(@"Custom album::%@",asset);
NSLog(@"Collection Result Count:%lu", (unsigned long)self.collectionResult.count);
//add assets to an array for later use in the uicollectionviewcell
}];https://stackoverflow.com/questions/39389899
复制相似问题