1

我正在尝试按年和月过滤 AlAssets。我已经可以获取日期并按年和月过滤,但是大约 1000 张照片太慢了。最好的方法是什么?

+ (void) loadFromLibraryByDate:(ALAssetsLibrary *)library assetType:(NSString *)type toArray:(NSMutableArray *)array onTable:(UITableView *)tableView onYear:(NSString *)year onMonth:(NSString *)mouth withDelegate:(id) delegate{

//clean passed objects
[array removeAllObjects];

// filter for the library
NSInteger groupType = ALAssetsGroupAll;



// block to enumerate thought the groups
ALAssetsLibraryGroupsEnumerationResultsBlock listGroupBlock =
^(ALAssetsGroup *group, BOOL *stop){
    if(group){
        [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop){
            if(asset){
                //                    cachedPhotos = [NSMutableDictionary new];

                if([asset valueForProperty:ALAssetPropertyType] == type){
                    if(year != nil && mouth != nil)
                    {
                        NSDate *date = [asset valueForProperty:ALAssetPropertyDate];
                        if(date.year == [year integerValue] && date.month == [mouth integerValue])
                        {
                            [array addObject:asset];
                        }
                     }
                    else if(year != nil && mouth == nil)
                    {
                        NSDate *date = [asset valueForProperty:ALAssetPropertyDate];
                        NSString *monthName = [date monthName:date.month];
                        if(date.year == [year integerValue])
                        {
                            if(![array containsObject:monthName])
                            {
                                [array addObject:monthName];
                            }
                        }
                    }
                    else
                    {
                        NSDate *date = [asset valueForProperty:ALAssetPropertyDate];
                        NSNumber *yearNum = [NSNumber numberWithInt:date.year];
                        if(![array containsObject:yearNum])
                        {
                            [array addObject:yearNum];
                        }
                    }
                }
            }
        }];
    }
    else{
        if( [delegate respondsToSelector:@selector(didFinishLoadingLibraryByDate:)] ){
            [delegate performSelector:@selector(didFinishLoadingLibraryByDate:)];
        }
        [tableView reloadData];
    }
};

// failure block, what happens if when something wrong happens when enumerating
ALAssetsLibraryAccessFailureBlock failBlock = ^(NSError *error){
    NSLog(@"Error: %@", [error localizedDescription]);

    static dispatch_once_t pred;

    dispatch_once(&pred, ^{

        dispatch_async(dispatch_get_main_queue(), ^{
            UIAlertView *libraryFailure = [[UIAlertView alloc] initWithTitle:@"Serviço de Localização" message:@"Para poder partilhar conteúdos nesta versão iOS, tem de autorizar os serviços de localização. (Definições > Serviços de Localização)" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [libraryFailure show];
            [libraryFailure release];
        });

    });

};

[library enumerateGroupsWithTypes:groupType usingBlock:listGroupBlock failureBlock:failBlock];

任何帮助表示赞赏,谢谢

4

2 回答 2

0

我认为你在正确的轨道上。除了枚举您的操作方式外,我不知道如何过滤元数据。不幸的是,在 iOS 上枚举资产组本身就很慢——如果您认为 1000 个不好,请尝试 10k 或 20k 个资产(这并不罕见,我现在在随身携带的手机上就有)。

解决此问题的一种方法(不一定建议,因为它需要大量工作并且潜在的错误非常高)是构建您自己的资产时间戳数据库。当用户忙于其他事情时(有教程或其他东西),枚举所有资产并将元数据复制为ALAssetPropertyAssetURL最适合您的格式。ALAssetsLibraryChangedNotification如果您这样做,请不要忘记收听消息。

于 2013-09-03T20:37:35.653 回答
0

您应该在应用程序启动时首先枚举所有 ALAsset,然后过滤它们。因为从数据库中枚举 ALAsset 非常慢,所以你不应该再次重新枚举它们。有一个通知,重新枚举 ALAsset 比第一个更快。苹果应该优化这个库。

于 2013-09-06T03:32:53.883 回答