LKAssetsLibrary 为 ALAssetsLibrary 提供分组/筛选/排序功能。
示例:按月份分组,用 JPEG 和 PNG 筛选,降序排列
LKAssetsCollection
|
|--LKAassetsCollectionEntry (May 2014)
| |
| |--LKAsset (JPEG, 25 May 2014 10:00)
| |--LKAsset (JPEG, 13 May 2014 12:00)
| |--LKAsset (JPEG, 4 May 2014 23:00)
| :
|
|--LKAassetsCollectionEntry (Apr 2014)
| |
| |--LKAsset (JPEG, 25 Apr 2014 10:00)
| |--LKAsset (JPEG, 13 APr 2014 12:00)
| |--LKAsset (JPEG, 4 Apr 2014 23:00)
| :
LKAssetsLibrary 有 3 个基本类
LKAssetsLibrary
LKAassetsGroup
LKasset
这些都是 ALAssetsLibrary、ALAssetsGroup、ALAsset 的包装类,它们有方便的方法(例如 LKAsset.thumbnail、LKAsset.date 等)。
LKAssetsLibrary 在获取资产后会发布通知。您可以通过观察通知来处理资产。
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(_assetsLibraryDidSetup:)
name:LKAssetsLibraryDidSetupNotification
object:nil];
self.assetsLibrary = [LKAssetsLibrary assetsLibrary];
[self.assetsLibrary reload];
}
一旦收到通知,您就使用 LKAssetGroup 或 LKAsset。
- (void)_assetsLibraryDidSetup:(NSNotification*)notification
{
[self.tableView reloadData];
}
使用 LKAssetsGroup 为 UITableViewCell。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GroupCell" forIndexPath:indexPath];
LKAssetsGroup* assetsGroup = self.assetsLibrary.assetsGroups[indexPath.row];
cell.imageView.image = assetsGroup.posterImage;
cell.textLabel.text = assetsGroup.description;
return cell;
}
用于 UICollectionViewCell。
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
AssetCell* cell = (AssetCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"AssetCell"
forIndexPath:indexPath];
LKAsset* asset = [self.assetsCollection assetForIndexPath:indexPath];
cell.imageView.image = asset.thumbnail;
return cell;
}
LKAssetsCollection 包含一系列资产。它有一个 LKAssetsCollectionEntry 数组,该 entry 包含资产 (LKAsset 数组)。
LKAssetsCollection 的交互
@interface LKAssetsCollection : NSObject
@property (nonatomic, weak , readonly) LKAssetsGroup* group;
@property (nonatomic, strong, readonly) NSArray* entries; // <lkassetscollectionentry>
@property (nonatomic, strong, readonly) id <LKAssetsCollectionGrouping> grouping;
@property (nonatomic, strong) id <LKAssetsCollectionFilter> filter;
@property (nonatomic, strong) id <LKAssetsCollectionSorter> sorter;
+ (instancetype)assetsCollectionWithGroup:(LKAssetsGroup*)group grouping:(id <LKAssetsCollectionGrouping>)grouping;
@end
@class LKAsset;
@interface LKAssetsCollection (NSIndexPath)
- (LKAsset*)assetForIndexPath:(NSIndexPath*)indexPath;
@end
资产通过 3 个因素提取
<LKAssetsCollectionGrouping> Yearly, Monthly,...
<LKAssetsCollectionFilter> JPEG, PNG, Screenshot,...
<LKAssetsCollectionSorter> Date in ascending or descending
这些定义为协议。库在这里提供通用类。
LKAssetsCollectionGenericGrouping
LKAssetsCollectionGenericFilter
LKAssetsCollectionGenericSorter
您可以像下面这样使用
self.assetsCollection = [LKAssetsCollection assetsCollectionWithGroup:self.assetsGroup
grouping:[LKAssetsCollectionGenericGrouping groupingWithType:self.groupingType]];
self.assetsCollection.filter = [LKAssetsCollectionGenericFilter filterWithType:LKAssetsCollectionGenericFilterTypeJPEG|LKAssetsCollectionGenericFilterTypePNG];
self.assetsCollection.sorter = [LKAssetsCollectionGenericSorter sorterAscending:NO];
LKAssetsCollectionGrouping 和 LKAssetsGroup 在初始化后不能更改,可以在初始化时更改。LKAssetsFilter 和 LKAssetsSorter 在初始化后可以更改。
列出 LKAssetsGroup 的信息。
显示 LKAssetsCollection 的信息。您可以尝试各种分组、筛选和排序。
iOS7
Hiroshi Hashiguchi, [email protected]
LKAssetsLibrary 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。