0

如何使用布尔集突破 ALAssetsLibrary enumerateAssets 方法的枚举。我可以跳出循环吗?

代码:

[self.library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
    @try {
        if(group != nil) {
            @autoreleasepool {
                    int newNumberOfPhotos = [group numberOfAssets];
                    if (self.numberOfPhotosInSavedPhotos < newNumberOfPhotos) {
                        //only new photos

                        NSRange range = NSMakeRange(self.numberOfPhotosInSavedPhotos, newNumberOfPhotos-self.numberOfPhotosInSavedPhotos);
                        NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:range];
                        [group enumerateAssetsAtIndexes:indexSet options:0 usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
                            @autoreleasepool {
                               if(someCondition)  {
 //get out of the enumeration block (that is, exit the method) or go to complete block
                                }

                                NSString *assetType = [result valueForProperty:ALAssetPropertyType];
                            }
                        } ];
               }         
            }
        } else {
            //enumeration ended

        }

    }
    @catch (NSException *e) {
        NSLog(@"exception streaming: %@", [e description]);
    }
}failureBlock:^(NSError *error){
    NSLog(@"Error retrieving albums stream: %@", [error description]);
    if (error.code==-3312  || error.code==-3311) {
    }
}];
4

1 回答 1

2

要停止资产枚举,只需*stop = YES在枚举块中设置。

如果要停止外部和内部枚举,请为停止变量使用不同的名称并将两者都设置为YES

[self.library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *outerStop) {
    ...
    [group enumerateAssetsAtIndexes:indexSet options:0 usingBlock:^(ALAsset *result, NSUInteger index, BOOL *innerStop) {
         if (someCondition) {
             *innerStop = YES;
             *outerStop = YES;
         } else {
             // process asset
         }
     }
 }

备注:@try/@catch如果循环中没有编程错误,通常不需要该块。

您对“新照片”的检查看起来很可疑,因为每个组中的资产数量与相同的数字进行比较self.numberOfPhotosInSavedPhotos,也许您应该再次检查该部分。

于 2013-04-01T14:22:40.833 回答