2014-02-11 67 views
1

我有NSArray,它包含我的庫中的所有ALAssets。我需要通過這些資產的名稱來過濾它。我試着創建第二個數組只與資產的名稱(通過ALAssetRepresention)。但之後我無法顯示縮略圖並保存資源鏈接。 那麼我該如何解決這個問題?ALAssets的NSPredicate和NSArray

回答

1

您需要將所有對象(如姓名,縮略圖和網址)存儲在一個對象中,然後將所有這些對象存儲在一個數組中。然後用這個代碼:

NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"ItemTitle" 
                 ascending:YES 
                 selector:@selector(localizedStandardCompare:)]; 

[arrData sortUsingDescriptors:[NSArray arrayWithObject:sorter]]; 

我存儲在對象中包含的NSString名稱爲「ItemTitle」

+0

我不確定我是否理解你的想法。 :(我不需要排序這些項目,我需要過濾它的搜索視圖。 –

1

我解決這個問題,接下來的路名。 我創建了包含2個值的對象SearchItemsNSString *nameALAsset *asset。 當我列舉的庫文件,我設置的值:

ALAssetRepresentation *rep = [result defaultRepresentation]; 

SearchItem *newItem = [[SearchItem alloc] init]; 
[newItem setName:[rep filename]]; 
[newItem setAsset:result]; 

然後我將該產品添加到我的NSMutableArray[assetsItems addObject:newItem];

,最後我用NSPredicate通過name過濾我的數組:

NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name CONTAINS[cd] %@", searchText]; 
    _searchResults = [self.assetsItems filteredArrayUsingPredicate:resultPredicate]; 

現在陣列準備取回。表視圖示例:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellIdentifier = @"SearchCell"; 

    SearchCell *cell = (SearchCell *)[self.tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    // Configure the cell... 
    if (cell == nil) { 
     cell = [[SearchCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    } 

    // Configure the cell... 
    SearchItem *searchItem = [_searchResults objectAtIndex:indexPath.row]; 
    ALAsset *currentAsset = searchItem.asset; 
    ALAssetRepresentation *rep = [currentAsset defaultRepresentation]; 

    [cell.nameLabel setText:[rep filename]]; 
    [cell.thumbnail setImage:[UIImage imageWithCGImage:[currentAsset thumbnail]]]; 

    return cell; 
} 

就是這樣。我希望將來它可以幫助別人。