打破照片ALAssetsLibrary的迭代

时间:2013-04-01 10:12:18

标签: iphone ios enumeration break alassetslibrary

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

CODE:

[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) {
    }
}];

1 个答案:

答案 0 :(得分: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进行比较,或许您应该再次检查该部分。