Reputation: 5902
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"];
}
}
}
}
}
Upvotes: 5
Views: 7349
Reputation: 756
The bitmask won't work as PHAssetCollectionTypeAlbum, PHAssetCollectionTypeSmartAlbum and PHAssetCollectionTypeMoment are not specified as bit fields. They take values 1, 2, 3. For a bitmask to work they would have to take values 1, 2, 4, which each value being on a different bit.
Upvotes: 0
Reputation: 151
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;
}
}
Upvotes: 11
Reputation: 5762
I think what you might be using is localizedLocationNames property of PHCollectionList. You should use localizedTitle under PHAssetCollection as you have mentioned in the question.
I wrote a little utility class and added the following method to it for retrieving album name.
+ (NSString *)localizedTitleForGroupOrCollection:(id)collection {
NSString *title = nil;
if ([PHAsset class]) {
title = [collection localizedTitle];
} else {
title = [collection valueForProperty:ALAssetsGroupPropertyName];
}
return title;
}
Upvotes: 0