iosswiftphotos

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

Solution

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