0
votes

im trying to present a list of all the albums titles in my iphone and when i press a title i want it to present all the photos of that specific album in a collection view.I am using the "PhotosUI" framework and i use this code to get the album titles:

-(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];
}

and this code to get all the photos for the specific album by it's title:

-(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];
}

for certain albums it's working, albums like "Instagram" and albums i have created with my phone but when i press the "Camera Roll" and "My Photo Stream" album it dosent show anything.

any ideas why this is happening or if there another way to fetch all of my device's photos?

1

1 Answers

2
votes

I got an answer. I retrieve all Camera Roll Photos&Videos. Below is my code.

   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
}];