Reputation: 2950
I am trying to retrieve all photo album names and all photos with that album name. The album names I got from [group valueForProperty:ALAssetsGroupPropertyName]
are mostly correct but there was one album name that says (null)
and the album called Favorites
does not show.
Album names array:
((null), Camera Roll, Last Import, Nissan Juke, Cam)
Here's my 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)
{
[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]]];
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 would like to know why there is a (null)
album name and why the album name Favorites
does not show.
Thanks.
Upvotes: 0
Views: 347
Reputation: 10479
When the enumeration is done, enumerationBlock
is invoked with group set to nil, so you should check each time the group
is nil or not and execute reloadData
only on the enumeration is done. The proper logic should look like this:
[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);
}];
For the Favorites
, it's a new feature in PhotoKit
for iOS8:
With iOS 8, Apple has given us PhotoKit, a modern framework that’s more performant than AssetsLibrary and provides features that allow applications to work seamlessly with a device’s photo library.
Favorite and Hidden Assets
To find out if an asset was marked as favorite or was hidden by the user, just inspect the favorite and hidden properties of the PHAsset instance.
See also: The Photos Framework
So you have to show the Favorites
via the PhotoKit
framework, check the official sample project: ExampleappusingPhotosframework.zip.
Upvotes: 1