如何将图像从最新到最旧获取到collectionviewe

How to fetch images from latest to oldest to collectionviewe

我有一个集合视图,它从 library.Currently 加载所有图像 listed.But 已列出最旧的图像 first.I 希望从最新显示到 oldest.How 显示它们这样做??

下面的代码是我目前using.How更新此代码以便从最新到最旧列出图像

 if PHPhotoLibrary.authorizationStatus() == PHAuthorizationStatus.Authorized
    {
        let collection:PHFetchResult = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .SmartAlbumUserLibrary, options: nil)

        var i = 0
        repeat
        {
            if (collection.count > 0)
            {
                if let first_Obj:AnyObject = collection.objectAtIndex(i)
                {
                    self.assetCollection = first_Obj as! PHAssetCollection
                }
                i += 1
            }
        }while( i < collection.count)
    }

您可以利用 PHFetchOptions.

PHFetchOptions Class Reference>sortDescriptors

试试这个:

        let options = PHFetchOptions()
        options.sortDescriptors = [NSSortDescriptor(key: "endDate", ascending: false)]
        let collection:PHFetchResult = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .SmartAlbumUserLibrary, options: options)

Swift 4.2

let allPhotosOptions = PHFetchOptions()
allPhotosOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
fetchResult = PHAsset.fetchAssets(with: allPhotosOptions)