【发布时间】:2014-02-11 07:36:08
【问题描述】:
我有 NSArray,其中包含我的库中的所有 ALAsset。我需要按这些资产的名称过滤它。我尝试仅使用资产名称(通过 ALAssetRepresention)创建第二个数组。但是后来我无法显示他们的缩略图并保存到资产的链接。 那么我该如何解决呢?
【问题讨论】:
标签: ios filter nspredicate alasset
我有 NSArray,其中包含我的库中的所有 ALAsset。我需要按这些资产的名称过滤它。我尝试仅使用资产名称(通过 ALAssetRepresention)创建第二个数组。但是后来我无法显示他们的缩略图并保存到资产的链接。 那么我该如何解决呢?
【问题讨论】:
标签: ios filter nspredicate alasset
您需要将所有对象(如名称、缩略图和网址)存储在一个对象中,然后将所有这些对象存储在一个数组中。然后使用此代码:
NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"ItemTitle"
ascending:YES
selector:@selector(localizedStandardCompare:)];
[arrData sortUsingDescriptors:[NSArray arrayWithObject:sorter]];
我将名称存储在包含名称为“ItemTitle”的 NSString 的对象中
【讨论】:
接下来我会解决这个问题。
我创建了对象SearchItems,其中包含两个值:NSString *name 和ALAsset *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;
}
就是这样。我希望它会在将来对某人有所帮助。
【讨论】: