assetLibrary一个简单的错误? [英] assetsLibrary a simple bug?

查看:68
本文介绍了assetLibrary一个简单的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的迷路了. 为什么我为每个UIImage两次获得NSLog?

I am really lost . why i get NSLog twice for each 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 !!   ********

                }];
            }
        }

推荐答案

由于某种原因,enumerateAssetsAtIndexes(和enumerateAssetsUsingBlock)在结束时使用result == nilindex == NSNotFound对该块进行了额外的调用.枚举.如果将NSLog()更改为

For some reason, enumerateAssetsAtIndexes (and enumerateAssetsUsingBlock) do an additional invocation of the block with result == nil and index == NSNotFound at the end of the enumeration. This becomes obvious if you change the NSLog() to

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值:

Therefore you have to check the value of result and ignore a nil value:

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];
          }
      }];
}

请注意,您可以将枚举简化为

Note that you can simplify the enumeration to

[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
     if (result != nil) {
         UIImage *thumbnail = [UIImage imageWithCGImage:[result thumbnail]];
         [assets addObject:thumbnail];
     }
}];

这篇关于assetLibrary一个简单的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆