简体   繁体   中英

iOS - Get Camera Roll images from ALAssetsLibrary

I managed to get photos from **ALAssetsLibrary** with this code:

-(void)getPhotosFromAssetsLibWithPhotoFilter:(NSString *)filterAlbumString
{
    _assets = [@[] mutableCopy];
    __block NSMutableArray *tmpAssets = [@[] mutableCopy];
    __block NSMutableArray *albumGroup = [@[] mutableCopy];
    ALAssetsLibrary *assetsLibrary = [PhotoLibViewController defaultAssetsLibrary];
    [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop)
    {
        if (group != nil)
        {
            [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop)
             {
                 if(result)
                 {
                     if (![filterAlbumString isEqualToString:@""])
                     {
                         if ([[NSString stringWithFormat:@"%@", [group valueForProperty:ALAssetsGroupPropertyName]] isEqualToString:filterAlbumString])
                         {
                             [tmpAssets addObject:result];
                         }
                     }
                     else
                     {
                         [tmpAssets addObject:result];
                     }
                 }
             }];

            [albumGroup addObject:[NSString stringWithFormat:@"%@", [group valueForProperty:ALAssetsGroupPropertyName]]];
        }
        else
        {
            dispatch_async(dispatch_get_main_queue(), ^{

                if ([self respondsToSelector:@selector(retrievedPhotoLibrary:)])
                {
                    NSArray *albumGroupReversed = [[albumGroup reverseObjectEnumerator] allObjects];
                    [self retrievedPhotoLibrary:albumGroupReversed];
                }
            });

            self.assets = [[tmpAssets reverseObjectEnumerator] allObjects];

            [self.collectionView reloadData];
        }

    } failureBlock:^(NSError *error) {
        NSLog(@"Error loading images %@", error);
    }];
}

I use it like this:

[self getPhotosFromAssetsLibWithPhotoFilter:@"Camera Roll"];

This works great. But the problem is I am localising my app and other languages that is not English does not use "Camera Roll" as the name of the album. I get no images when I use @"Camera Roll" .

Is there a name to use that represents Camera Roll ? That will work on every device no matter the language?

请使用ALAssetsGroupSavedPhotos进行过滤。

Try something like this,

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


[al enumerateGroupsWithTypes:ALAssetsGroupAll

                  usingBlock:^(ALAssetsGroup *group, BOOL *stop)
 {
     [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
      {
          if (asset)
          {
             // .. do something with the asset
                  }
      }
      ];
 }

                failureBlock:^(NSError *error)
 {
     // User did not allow access to library
     //.. handle error
 }
 ];

you can use different source by replacing enumerateGroupsWithTypes .

Second thing ALassetlibrary is deprecated now so you should try PHPhotoLibrary .

Hope this will help :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM