我正在使用 Photos 框架通过这段代码在 iOS8 中获取相册列表,
如何重新排序 smartAlbums,以便我可以在所有顶部显示“最近添加”
let smartAlbums : PHFetchResult = PHAssetCollection.fetchAssetCollectionsWithType(PHAssetCollectionType.SmartAlbum, subtype: PHAssetCollectionSubtype.AlbumRegular, options: nil)
在 cellForRow 方法中
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let collection = smartAlbums[indexPath.row]
cell.textLabel?.text = collection.localizedTitle
return cell
}
let photofetchOpt:PHFetchOptions? = PHFetchOptions()
photofetchOpt?.sortDescriptors = [NSSortDescriptor(key:"localizedTitle", ascending: false)]
我尝试在获取 AssetCollections 时使用 PHFetchOptions,但对 smartAlbums 的顺序没有影响。
最佳答案
PHAssetCollection
类有一个属性名称 startDate
表示 Assets 集合中所有 Assets 中最早的创建日期。 Link .
在 PHFetchOptions
中使用 sortDescriptor
获取相册购买最新图像订单。
目标-C
PHFetchOptions *options = [PHFetchOptions new];
options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"startDate" ascending:NO]];
PHFetchResult<PHAssetCollection*> *allCollections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAny options:nil];
swift
let options = PHFetchOptions()
options.sortDescriptors = [NSSortDescriptor(key: "startDate", ascending: false)]
let result = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .any, options: options)
关于iphone - 如何更改 PHAssetCollection 列表的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33478419/