I've been trying to find an alternative for getting the album names with the Photos API in iOS 8. With ALAssets, we can use: valueForProperty:ALAssetsGroupPropertyName
however with the Photos API, I can't seem to find an alternative. There is: localizedTitle
under PHAssetCollection
but that isn't right either, it just gives me the city names. I'm looking for something that can return the actual names of the groups, including ones synced with iTunes.
I'd appreciate any help to see how you do this in your apps. Apple is encouraging us to only use the Photos API for apps linked with 8.0, so I'd rather not use both ALAssetLibrary and Photos.
Code:
- (NSString *)nameForAlbumInCollection:(id)collection
{
NSString *title = nil;
if ([PHAsset class])
{
title = [collection localizedTitle];
}
else
{
title = [collection valueForProperty:ALAssetsGroupPropertyName];
}
return title;
}
- (void)setup
{
self.recentsCollectionDataSource = [[NSMutableOrderedSet alloc]init];
self.favoritesCollectionDataSource = [[NSMutableOrderedSet alloc]init];
self.albumsTableDataSource = [[NSMutableOrderedSet alloc]init];
NSMutableArray *segmentTitles = [[NSMutableArray alloc]init];
self.assetsFetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum | PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:nil];
if (!self.parentController.canTakeOrChooseVideo)
{
fetchOptions.predicate = [NSPredicate predicateWithFormat:@"mediaType = %i",PHAssetMediaTypeImage];
}
for (PHAssetCollection *sub in self.assetsFetchResult)
{
PHFetchResult *assetsInCollection = [PHAsset fetchAssetsInAssetCollection:sub options:nil];
for (PHAsset *asset in assetsInCollection)
{
NSLog(@"%@",[self nameForAlbumInCollection:sub]);
[self.recentsCollectionDataSource addObject:asset];
if (![segmentTitles containsObject:@"Recents"])
{
[segmentTitles addObject:@"Recents"];
[segmentTitles addObject:@"Albums"];
}
if (asset.isFavorite)
{
[self.favoritesCollectionDataSource addObject:asset];
if (![segmentTitles containsObject:@"Favorites"])
{
[segmentTitles addObject:@"Favorites"];
}
}
}
}
}
This is how I made a list of the album names in a project of mine. You may have to deviate a bit, but this should work.
NSArray *collectionsFetchResults;
NSMutableArray *localizedTitles = [[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];
// 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];
localizedTitles[x] = collection.localizedTitle;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With