How to fetch images from latest to oldest to collectionviewe

I have a collectionview which loads from library.Currently all the images are listed.But oldest images are listed first.I wish to display them from latest showing first to oldest.How to do this??

The code below is what I am currenly using.How to update this inorder to list images from starting latest to oldest

 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)
    }

Upvotes: 0

Views: 320

Answers (2)

ilkayaktas
ilkayaktas

Reputation: 188

Swift 4.2

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

Upvotes: 1

OOPer
OOPer

Reputation: 47896

You may utilize PHFetchOptions.

PHFetchOptions Class Reference>sortDescriptors

Try this:

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

Upvotes: 1

Related Questions