swift2 如何更改PHAssetCollection列表的顺序

6jygbczu  于 2022-11-06  发布在  Swift
关注(0)|答案(2)|浏览(170)

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

我曾尝试在获取资产集合时使用PHFetchOptions,但对智能相册的顺序没有影响。

nwo49xxi

nwo49xxi1#

PHAssetCollection类具有属性名称startDate,该属性名称指示资产集合中所有资产中最早的创建日期。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];

迅捷

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

vojdkbi02#

我得到了一个重新排序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
查看结果

相关问题