iOS - ALAssetsGroupPropertyName returns 空相册名称
iOS - ALAssetsGroupPropertyName returns null album name
我正在尝试检索所有相册名称和具有该相册名称的所有照片。我从 [group valueForProperty:ALAssetsGroupPropertyName]
得到的相册名称大部分是正确的,但有一个相册名称是 (null)
,而名为 Favorites
的相册没有显示。
专辑名称数组:
((null), Camera Roll, Last Import, Nissan Juke, Cam)
这是我的代码
-(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);
}];
}
我想知道为什么会有(null)
相册名称,为什么相册名称Favorites
不显示。
谢谢。
当枚举完成后,enumerationBlock
被调用,组设置为 nil,所以你应该在每次 group
是否为 nil 时检查并只在 reloadData
上执行枚举完成。正确的逻辑应该是这样的:
[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);
}];
对于 Favorites
,这是 iOS8 的 PhotoKit
中的新功能:
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
所以你必须通过PhotoKit
框架显示Favorites
,查看官方示例项目:ExampleappusingPhotosframework.zip.
我正在尝试检索所有相册名称和具有该相册名称的所有照片。我从 [group valueForProperty:ALAssetsGroupPropertyName]
得到的相册名称大部分是正确的,但有一个相册名称是 (null)
,而名为 Favorites
的相册没有显示。
专辑名称数组:
((null), Camera Roll, Last Import, Nissan Juke, Cam)
这是我的代码
-(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);
}];
}
我想知道为什么会有(null)
相册名称,为什么相册名称Favorites
不显示。
谢谢。
当枚举完成后,enumerationBlock
被调用,组设置为 nil,所以你应该在每次 group
是否为 nil 时检查并只在 reloadData
上执行枚举完成。正确的逻辑应该是这样的:
[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);
}];
对于 Favorites
,这是 iOS8 的 PhotoKit
中的新功能:
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
所以你必须通过PhotoKit
框架显示Favorites
,查看官方示例项目:ExampleappusingPhotosframework.zip.