如何更改PHAssetCollection列表的顺序

时间:2015-11-02 13:10:59

标签: iphone swift ios8 swift2 xcode7

我使用照片框架在iOS8中使用此代码获取相册列表,
如何重新排序smartAlbums,所以我可以在所有

之上显示'最近添加'
 let smartAlbums : PHFetchResult = PHAssetCollection.fetchAssetCollectionsWithType(PHAssetCollectionType.SmartAlbum, subtype: PHAssetCollectionSubtype.AlbumRegular, options: nil)

in 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的顺序没有影响。

2 个答案:

答案 0 :(得分:1)

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

sortDescriptor 中使用 PHFetchOptions 获取相册购买最新图片订单。

目标 C

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

迅捷

let options = PHFetchOptions()
options.sortDescriptors = [NSSortDescriptor(key: "startDate", ascending: false)]
let result = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .any, options: options)

答案 1 :(得分:0)

我有一个重新排序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

见结果

enter image description here