2013-04-01 38 views
0

如何突破ALAssetsLibrary enumerateAssets方法中使用布爾值集合的枚舉。我可以跳出循環嗎?中斷圖片的迭代ALAssetsLibrary

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

可以請你!更具體地說,你陷入困境並需要幫助?發佈相關代碼將有助於理解您的問題。 – swiftBoy

回答

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進行比較,或許您應該再次檢查該部分。

+0

非常感謝:) –