按年份删除alassets

时间:2013-09-02 14:38:58

标签: objective-c alassetslibrary

我正在尝试按年和月过滤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];

感谢任何帮助,谢谢

2 个答案:

答案 0 :(得分:0)

我认为你正走在正确的轨道上。除了通过枚举你的方式,我无法过滤元数据。不幸的是,通过资产组进行枚举在iOS上本质上很慢 - 如果你认为1000是坏的,尝试10k或20k资产(根本不常见,我现在在我的随身携带电话上有这个)。

解决这个问题的一种方法(不一定建议,因为它的大量工作和潜在的错误非常高)是建立自己的资产时间戳数据库。当用户忙碌时(使用教程或其他内容),枚举所有资产并将元数据和ALAssetPropertyAssetURL复制到最适合您的格式。如果你这样做,不要忘记听ALAssetsLibraryChangedNotification条消息。

答案 1 :(得分:0)

首先应在App启动时枚举所有ALAsset,然后对其进行过滤。因为从数据库枚举ALAsset非常慢,所以不应再次重新枚举它们。 有一个通知,重新计算ALAsset比第一个更快。 Apple应优化库。