How to access smart folder from gallery using ALAsset

57 Views Asked by At

I am making an app where I want to get the list of all albums name from gallery including smart folders(favorites, screenshots. this is an old app where we have used ALAsset in order to access the gallery in our app.

Is there any way through which we can access smart folders as well using ALAssetLibrary?

1

There are 1 best solutions below

1
user3619771 On

This code is help.

   #import <AssetsLibrary/AssetsLibrary.h>

    @property (nonatomic, strong) ALAssetsLibrary *_assetsLibrary;

    - (ALAssetsLibrary *)defaultAssetsLibrary {
        static dispatch_once_t pred = 0;
        static ALAssetsLibrary *library = nil;
        dispatch_once(&pred, ^{
            library = [[ALAssetsLibrary alloc] init];
        });
        return library;
    }

    -(void) getgalleryPic
    {
        if (self.photos == nil) {
            self.photos = [[NSMutableArray alloc] init];
        }else
        {
            [self.photos removeAllObjects];
        }

        PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];

        if (status == PHAuthorizationStatusAuthorized) {
            // Access has been granted.
            NSMutableArray *collector = [[NSMutableArray alloc] initWithCapacity:0];
            ALAssetsLibrary *library = [self defaultAssetsLibrary];

            [library enumerateGroupsWithTypes:ALAssetsGroupAll
                                   usingBlock:^(ALAssetsGroup *group, BOOL *stop)
             {
                 [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
                  {
                      if (asset) {
                          [collector addObject:asset];
                      }else
                      {
                          self.photos = [[[collector reverseObjectEnumerator] allObjects] mutableCopy];
                          NSLog(@"photo lenght %lu",(unsigned long)[self.photos count]);
                          [_collectionVW reloadData];
                      }
                  }];
             }
                                 failureBlock:^(NSError *error) { NSLog(@"Boom!!!");}
             ];
        }

        else if (status == PHAuthorizationStatusDenied) {
            // Access has been denied.
        }

        else if (status == PHAuthorizationStatusNotDetermined) {

            // Access has not been determined.
            [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {

                if (status == PHAuthorizationStatusAuthorized) {
                    // Access has been granted.
                    NSMutableArray *collector = [[NSMutableArray alloc] initWithCapacity:0];
                    ALAssetsLibrary *library = [self defaultAssetsLibrary];

                    [library enumerateGroupsWithTypes:ALAssetsGroupAll
                                           usingBlock:^(ALAssetsGroup *group, BOOL *stop)
                     {
                         [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
                          {
                              if (asset) {
                                  [collector addObject:asset];
                              }else
                              {
                                  self.photos = [[[collector reverseObjectEnumerator] allObjects] mutableCopy];
                                  NSLog(@"photo lenght %lu",(unsigned long)[self.photos count]);
                                  [_collectionVW reloadData];
                              }
                          }];
                     }
                                         failureBlock:^(NSError *error) { NSLog(@"Boom!!!");}
                     ];
                }

                else {
                    // Access has been denied.
                }
            }];
        }    else if (status == PHAuthorizationStatusRestricted) {
            // Restricted access - normally won't happen.
        }


    }