3

私は本当に迷子になっています。なぜNSLogをそれぞれ2回取得するのUIImageですか?

 //------ get the images from the camera roll ----------
    assets=[[NSMutableArray alloc]init];
    NSMutableArray *cameraRollPictures=[[NSMutableArray alloc]init];
    ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
    [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop)
    {

        NSInteger numberOfAssets = [group numberOfAssets];
        NSLog(@"NUM OF IMAGES:%d",numberOfAssets);
        if (numberOfAssets > 0)
        {


            for (int i = 0; i <= numberOfAssets-1; i++)
            {

                [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:i] options:0 usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop)
                 {
                    UIImage *thumbnail = [UIImage imageWithCGImage:[result thumbnail]];
                    [assets addObject:thumbnail];
                     NSLog(@"theObject!!!! -- (%d) %@",i,thumbnail);

            //******* for each i its here twice !!   ********

                }];
            }
        }
4

1 に答える 1

6

何らかの理由で、enumerateAssetsAtIndexes(および)列挙を使用して、列挙の最後にブロックをenumerateAssetsUsingBlock追加で呼び出します。NSLog()を次のように変更すると、これが明らかになります。result == nilindex == NSNotFound

NSLog(@"i=%d, index=%ld, result=%@", i, (unsigned long)index, result);

次に、出力を取得します

NUM OF IMAGES:2
i=0, index=0, result=ALAsset - Type:Photo, URLs:assets-library://asset/asset.PNG?id=...
i=0, index=2147483647, result=(null)
i=1, index=1, result=ALAsset - Type:Photo, URLs:assets-library://asset/asset.PNG?id=...
i=1, index=2147483647, result=(null)

したがって、の値を確認し、値をresult無視する必要がありnilます。

for (int i = 0; i <= numberOfAssets-1; i++) {
     [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:i] options:0 usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop)
      {
          if (result != nil) {
              UIImage *thumbnail = [UIImage imageWithCGImage:[result thumbnail]];
              [assets addObject:thumbnail];
          }
      }];
}

列挙を単純化できることに注意してください

[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
     if (result != nil) {
         UIImage *thumbnail = [UIImage imageWithCGImage:[result thumbnail]];
         [assets addObject:thumbnail];
     }
}];
于 2013-02-26T19:08:09.160 に答える