AlAssetLibrary返回空结果 [英] AlAssetLibrary returning empty results

查看:141
本文介绍了AlAssetLibrary返回空结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试挑选一些相机胶卷元数据。当我枚举资产时,我似乎无法检索任何信息并得到一个空数组。有没有我缺少的一步?

I'm trying to pick out some camera roll meta data. When I enumerate through the assets, I cannot seem to retrieve any information and get an empty array. Is there a step I'm missing?

我的代码:

assets = [[NSMutableArray array] init]; 

void (^assetEnumerator)(ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *asset, NSUInteger index, BOOL *stop) {
    if(asset != NULL) {
        [assets addObject:asset];
        dispatch_async(dispatch_get_main_queue(), ^{
        });
    }
};

void (^assetGroupEnumerator)(ALAssetsGroup *, BOOL *) =  ^(ALAssetsGroup *group, BOOL *stop) {
    if(group != nil) {
        [group enumerateAssetsUsingBlock:assetEnumerator];
    }
};

library = [[ALAssetsLibrary alloc] init];

[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos 
                       usingBlock:assetGroupEnumerator
                     failureBlock: ^(NSError *error) {
                         NSLog(@"Failed.");
                     }];    

NSLog(@"%@", assets); //prints an empty array


推荐答案

Midhun MP是对的您不是在等待异步枚举完成。在这种情况下,您有异步块调用其他异步块,因此知道何时完成所有枚举并不简单。

Midhun MP is right that you are not waiting for the asynchronous enumeration to complete. In this case, you have asynchronous blocks calling other asynchronous blocks, so it is not simple to know when all enumeration is done.

如果您想知道何时完成此操作,并最终得到一个包含所有枚举资产的数组,您可以使用dispatch_groups。这是你可以做到的一种方式(我已经包含了多个ALAssetGroup类型来表明这可以用于多个专辑):

If you would like to know when this is done, and end up with an array that contains all of the enumerated assets, you could use dispatch_groups. Here is one way you could do that (I've included multiple ALAssetGroup types to show that this can work with multiple albums):

dispatch_group_t loadingGroup = dispatch_group_create();
NSMutableArray * assets = [[NSMutableArray array] init];
NSMutableArray * albums = [[NSMutableArray array] init];

void (^assetEnumerator)(ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *asset, NSUInteger index, BOOL *stop) {
    if(index != NSNotFound) {
        [assets addObject:asset];
        dispatch_async(dispatch_get_main_queue(), ^{ });
    } else {
        dispatch_group_leave(loadingGroup);
    }
};


void (^assetGroupEnumerator)(ALAssetsGroup *, BOOL *) =  ^(ALAssetsGroup *group, BOOL *stop) {
    if(group != nil) {
        [albums addObject: group];
    } else {
         NSLog(@"Found %d albums", [albums count]);
         // album loading is done
        for (ALAssetsGroup * album in albums) {
            dispatch_group_enter(loadingGroup);
            [album enumerateAssetsUsingBlock: assetEnumerator];
        }
        dispatch_group_notify(loadingGroup, dispatch_get_main_queue(), ^{
            NSLog(@"DONE: ALAsset array contains %d elements", [assets count]);
        });
    }
};

ALAssetsLibrary * library = [[ALAssetsLibrary alloc] init];

[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos | ALAssetsGroupAlbum
                   usingBlock:assetGroupEnumerator
                 failureBlock: ^(NSError *error) {
                     NSLog(@"Failed.");
                 }];

(在此示例中,将各种块添加到资产是安全的专辑因为枚举完全发生在主线程上。)

(In this example, it is safe to have various blocks adding to assets and albums because the enumeration is all happening on the main thread.)

这篇关于AlAssetLibrary返回空结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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