我枚举ALAssetsLibrary以了解我的照片库有哪些组,
如何知道块何时停止枚举?
NSMutableArray *albumsArray = [NSMutableArray array];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock: ^(ALAssetsGroup *group, BOOL *stop){
if ([group valueForProperty:ALAssetsGroupPropertyName] != nil) {
[albumsArray addObject:[group valueForProperty:ALAssetsGroupPropertyName]];
}
NSLog(@"tu arraya :: %@", albumsArray);
//tha pics?
[group enumerateAssetsUsingBlock: ^(ALAsset *result, NSUInteger index, BOOL *stop){
// ---> ALAsset:
NSLog(@" Stop? %@", (stop ? @"YES" : @"NO") );
}];
}
failureBlock: ^(NSError *error) {
NSLog(@"Failure");
}];
NSLog(@"tu arraya 1:: %@", albumsArray);
答案 0 :(得分:2)
在if (group == nil)
声明后检查enumerateAssetsUsingBlock
。
见下文:
NSMutableArray *albumsArray = [NSMutableArray array];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock: ^(ALAssetsGroup *group, BOOL *stop){
if ([group valueForProperty:ALAssetsGroupPropertyName] != nil) {
[albumsArray addObject:[group valueForProperty:ALAssetsGroupPropertyName]];
}
NSLog(@"tu arraya :: %@", albumsArray);
//tha pics?
[group enumerateAssetsUsingBlock: ^(ALAsset *result, NSUInteger index, BOOL *stop){
// ---> ALAsset:
NSLog(@" Stop? %@", (stop ? @"YES" : @"NO") );
}];
// this is the end of enumeration
if (group == nil)
{
NSLog(@"THE END!!!");
}
}
failureBlock: ^(NSError *error) {
NSLog(@"Failure");
}];
NSLog(@"tu arraya 1:: %@", albumsArray);
另外,你应该考虑分解你的块以使事情更容易阅读。例如:
NSMutableArray *albumsArray = [NSMutableArray array];
ALAssetsLibrary *library = [BMBAssetManager defaultAssetsLibrary];
void (^assetEnumerator)( ALAsset *, NSUInteger, BOOL *) =
^(ALAsset *result, NSUInteger index, BOOL *stop)
{
// ---> ALAsset:
NSLog(@" Stop? %@", (stop ? @"YES" : @"NO") );
};
void (^assetGroupEnumerator)( ALAssetsGroup *, BOOL *) =
^(ALAssetsGroup *group, BOOL *stop){
if ([group valueForProperty:ALAssetsGroupPropertyName] != nil) {
[albumsArray addObject:[group valueForProperty:ALAssetsGroupPropertyName]];
}
NSLog(@"tu arraya :: %@", albumsArray);
//tha pics?
[group enumerateAssetsUsingBlock:assetEnumerator];
// this is the end of enumeration
if (group == nil)
{
NSLog(@"THE END!!!");
}
};
[library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:assetGroupEnumerator
failureBlock: ^(NSError *error) {
NSLog(@"Failure");
}];