Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPhotoLibrary get album names

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"];
                }
            }
        }
    }
}
like image 673
klcjr89 Avatar asked Jun 02 '15 20:06

klcjr89


1 Answers

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;

    }
}
like image 105
Hpennington Avatar answered Nov 10 '22 13:11

Hpennington