10

How do I get a list of all the photo albums in iOS? I know the image picker has to have a list of them somewhere since it displays them all to the user. Once I have the photo albums, how would I go about displaying thumbnails of all the images in a particular album? I can't seem to find anything in the docs, but I'm sure there's got to be a way to do it.

nick
  • 2,833
  • 3
  • 34
  • 61
  • In my project i get some albums it is display date instead of name . i debug my code and i found my albums list contains event albums and that contains no of albums and its name like date. any idea to skip this event form my asset array ?. please give me solution if u have . Thanks in Advance – Ilesh P Feb 18 '15 at 07:01

3 Answers3

20

You can't get that kind of information from UIImagePickerController if that is what you are asking.

Take a look at AssetsLibrary framework. There is even sample code that implements a custom image picker.

Filip Radelic
  • 26,607
  • 8
  • 71
  • 97
  • In my project i get some albums it is display date instead of name . i debug my code and i found my albums list contains event albums and that contains no of albums and its name like date. any idea to skip this event form my asset array ?. please give me solution if u have . Thanks in Advance – Ilesh P Feb 18 '15 at 07:00
19

Enumerate and get latest image and its thumbnail.

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

// Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos.
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

    // Within the group enumeration block, filter to enumerate just photos.
    [group setAssetsFilter:[ALAssetsFilter allPhotos]];

    // Chooses the photo at the last index
    [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {
        // The end of the enumeration is signaled by asset == nil.
        if (alAsset) {
            ALAssetRepresentation *representation = [alAsset defaultRepresentation];
            UIImage *latestPhoto = [UIImage imageWithCGImage:[representation fullScreenImage]];


            UIImage *latestPhotoThumbnail =  [UIImage imageWithCGImage:[alAsset thumbnail]];


            // Stop the enumerations
            *stop = YES; *innerStop = YES;

            // Do something interesting with the AV asset.
            //[self sendTweet:latestPhoto];
        }
    }];
} failureBlock: ^(NSError *error) {
    // Typically you should handle an error more gracefully than this.
    NSLog(@"No groups");
}];
andrewchan2022
  • 4,953
  • 45
  • 48
  • Instead of enumerating for the latest photo you could use the `posterImage` method like this `[group posterImage]`, which gives you a ref of the image you see when browsing the gallery, usually the first pic – Mostafa Berg Apr 05 '14 at 18:36
  • @ibsweek In my project i get some albums it is display date instead of name . i debug my code and i found my albums list contains event albums and that contains no of albums and its name like date. any idea to skip this event form my asset array ?. please give me solution if u have . Thanks in Advance – Ilesh P Feb 18 '15 at 07:01
0

The Assets Library framework is deprecated as of iOS 9.0. Instead, use the Photos framework, which in iOS 8.0 and later provides more features and better performance for working with a user’s photo library.

Check Photos framework, and inspect: PHAsset, PHAssetCollection, and PHCollectionList, depending on your needs.

Stephen H King
  • 256
  • 2
  • 5