iphone - 如何更改 PHAssetCollection 列表的顺序

标签 iphone swift ios8 swift2 xcode7

我正在使用 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

    }

enter image description here

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/

相关文章:

iphone - 用于紧急响应的 iOS 应用程序 - 如何在没有密码的情况下保护数据?

iphone - 闪存 CS5.5 : Photo is rotated when capture a photo using CameraUI on iPhone

ios - 在 iOS 中画一个方框

ios - SFSafariViewController 在导航栏和底部工具栏中是否始终具有相同的 UI 元素?

ios - 使用 GIDSignIn 处理登录另一个 Google 应用程序时不获取 Google 用户

ios - 从 sqlite3 数据库 iOS 7 中删除所有记录

iphone - iOS从nsmutablearray中删除对象并重新索引数组

ios - 从 UICollectionView 到 UILabel 的 myLabel 导出无效。 socket 无法连接到重复内容

xcode - iOS 8 UINavigationBar 背景图片

objective-c - UITableViewCell 选择突出显示颜色