【问题标题】:when the ALAssetsLibraryGroupsEnumerationResultsBlock function is trigger in ios当 ALAssetsLibraryGroupsEnumerationResultsBlock 函数在 ios 中被触发时
【发布时间】:2015-02-13 12:06:32
【问题描述】:

在我的应用程序中,我想从特定专辑中获取图像和视频。因此,我使用 ALAssetsLibraryGroupsEnumerationResultsBlock 函数从特定专辑中获取图像和视频。在这里,我将资产值存储在 nsmutablearray 中,我获取该数组值viewdidLoad,但在 viewdidload 中数组值为 null。所以当 ALAssetsLibraryGroupsEnumerationResultsBlock 被触发时。 下面是我的代码帮助我..

- (void)viewDidLoad {
    [super viewDidLoad];
if (self.assetLibrary == nil) {
        _assetLibrary = [[ALAssetsLibrary alloc] init];
    }
    if (self.groups == nil) {
        _groups = [[NSMutableArray alloc] init];
    } else {
        [self.groups removeAllObjects];
    }
[self loadAssetImages];
}
-(void)loadAssetImages{

    NSLog(@"Load  asset Image ");
    ALAssetsLibraryAccessFailureBlock failureBlock = ^(NSError *error) {
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"ERROR" message:[NSString stringWithFormat:@"No Albums Available"] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];
    };

    ALAssetsLibraryGroupsEnumerationResultsBlock listGroupBlock = ^(ALAssetsGroup *group, BOOL *stop) {
        NSLog(@"Block");
        if (group == nil)
        {
            return;
        }
        if ([[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString:@"FlipDemo"]) {
            [self.groups addObject:group];
            [self loadImages];
            return ;
        }
        if (stop) {
            return;
        }
    };

    NSLog(@"Block Out");
    [self.assetLibrary enumerateGroupsWithTypes:ALAssetsGroupAll
                           usingBlock: listGroupBlock
                         failureBlock:failureBlock];
}


-(void)loadImages{
     NSLog(@"Load Image000");
    ALAssetsGroup *assetGroup;
    for (assetGroup in self.groups)
    {
        for (int i = 0; i<[self.groups count]; i++)
        {
           [ assetGroup enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop)
            {
                if (result) {
                    if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo])
                    {
                        [videoOnly addObject:result];

                    }
                    else if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto])
                    {
                        [imgOnly addObject:result];
//                     NSLog(@"Photo");
                    }
                }
            }
            ];        
        }
    }
}

【问题讨论】:

    标签: ios image video alassetslibrary alasset


    【解决方案1】:

    该方法是异步的。您可以在帮助文档中找到功能详细信息:

    帮助文档

    • (void)enumerateGroupsWithTypes:(ALAssetsGroupType)types usingBlock:(ALAssetsLibraryGroupsEnumerationResultsBlock)enumerationBlock failureBlock:(ALAssetsLibraryAccessFailureBlock)failureBlock

    此方法是异步的。枚举组时,可能会要求用户确认应用程序对数据的访问;但是,该方法会立即返回。你应该对 enumerationBlock 中的资产执行任何你想做的工作。

    帮助文档结束

    因此,根据您的代码,您无法在主线程中获取任何内容。因为 iOS 在另一个线程中获取资产。

    你可以设置一个key-value观察,让主线程等待这个异步函数。

    1) 设置一个属性,例如:handleOverFindFlag 2) 执行函数前:设置handleOverFindFlag=NO 3) 添加观察者

    [self addObserver:self forKeyPath:@"handleOverFindFlag" options:0 context:NULL];
    

    4) 设置观察功能:

    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:
    
        (NSDictionary *)change context:(void *)context
        {
          if([keyPath isEqualToString:@""])
          {
             //handle your findings
          }
        }
    

    5) 这样做之后

    [self removeObserver:self forKeyPath:@"handleOverFindFlag"];
    

    您可以在帮助文档中找到有关 Key-Value 编码的详细信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-03
      • 2015-06-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多