我正在使用照片框架通过以下代码在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的顺序没有影响。
发布于 2021-04-10 19:09:06
PHAssetCollection类有一个名为startDate的属性,它表示资产集合中所有资产中最早的创建日期。Link。
使用PHFetchOptions中的sortDescriptor来获取相册,购买最新的图像订单。
Objective-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)发布于 2015-11-06 19:35:02
我有一个重新排序PHCollectionList的解决方案,我正在为那些正在努力重新排序列表的人发布这个答案,基本上我使用的是NSMutableArray sortedSmartAlbumsCollectionsFetchResults
if(smartAlbums.count > 0) {
for i in 0...smartAlbums.count-1 {
let assetCollection:PHAssetCollection = smartAlbums[i] as! PHAssetCollection
if assetCollection.assetCollectionSubtype == PHAssetCollectionSubtype.SmartAlbumRecentlyAdded {
sortedSmartAlbumsCollectionsFetchResults.insertObject(assetCollection, atIndex: 0)
}
else {
sortedSmartAlbumsCollectionsFetchResults.addObject(assetCollection)
}
}
}如上面的代码所示,我已经从smartAlbums中获取了每个PHAssetCollection,并检查它的子类型是否为SmartAlbumRecentlyAdded,如果是,则将其插入索引为0的位置,否则继续添加NSMutableArray
查看结果

https://stackoverflow.com/questions/33478419
复制相似问题