2014-10-01 59 views
0

林枚舉ALAssetsLibrary知道我的組照片庫有,如何知道什麼時候結束塊列舉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

它在文檔中告訴你'enumerateGroupsWithTypes:usingBlock:failureBlock:'。 – rmaddy 2014-10-01 03:11:53

+0

爲什麼要投票?這是顯而易見的嗎?,很高興躲在投票後面,並且沒有反饋! – MaKo 2014-10-01 03:14:44

+0

您有反饋意見。答案在該方法的文檔中。 – rmaddy 2014-10-01 03:15:44

回答

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"); 
        }]; 
相關問題