首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何更改PHAssetCollection列表的顺序

如何更改PHAssetCollection列表的顺序
EN

Stack Overflow用户
提问于 2015-11-02 21:10:59
回答 2查看 598关注 0票数 1

我正在使用照片框架通过以下代码在iOS8中获取相册列表。

如何重新排序smartAlbums,这样我就可以在最上面显示“最近添加的”

代码语言:javascript
复制
 let smartAlbums : PHFetchResult = PHAssetCollection.fetchAssetCollectionsWithType(PHAssetCollectionType.SmartAlbum, subtype: PHAssetCollectionSubtype.AlbumRegular, options: nil)

cellForRow方法中

代码语言:javascript
复制
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

            let collection = smartAlbums[indexPath.row]
            cell.textLabel?.text = collection.localizedTitle
            return cell

    }

代码语言:javascript
复制
let photofetchOpt:PHFetchOptions? = PHFetchOptions()
    photofetchOpt?.sortDescriptors = [NSSortDescriptor(key:"localizedTitle", ascending: false)]

我曾尝试在获取AssetCollections时使用PHFetchOptions,但对smartAlbums的顺序没有影响。

EN

回答 2

Stack Overflow用户

发布于 2021-04-10 19:09:06

PHAssetCollection类有一个名为startDate的属性,它表示资产集合中所有资产中最早的创建日期。Link

使用PHFetchOptions中的sortDescriptor来获取相册,购买最新的图像订单。

Objective-C

代码语言:javascript
复制
PHFetchOptions *options = [PHFetchOptions new];
options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"startDate" ascending:NO]];
PHFetchResult<PHAssetCollection*> *allCollections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAny options:nil];

Swift

代码语言:javascript
复制
let options = PHFetchOptions()
options.sortDescriptors = [NSSortDescriptor(key: "startDate", ascending: false)]
let result = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .any, options: options)
票数 1
EN

Stack Overflow用户

发布于 2015-11-06 19:35:02

我有一个重新排序PHCollectionList的解决方案,我正在为那些正在努力重新排序列表的人发布这个答案,基本上我使用的是NSMutableArray sortedSmartAlbumsCollectionsFetchResults

代码语言:javascript
复制
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

查看结果

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33478419

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档