PHFetchOptions for PHAsset in same sort order as Photos.app

I have used the following code to get the list of smart albums which includes the "Camera Roll":

// get the smart albums
PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
for (NSInteger i = 0; i < smartAlbums.count; i++) {
    PHAssetCollection *assetCollection = smartAlbums[i];
    // Then use the following to get the photo images:
    PHFetchResult *assetsFetchResult = [PHAsset fetchAssetsInAssetCollection:assetCollection options:nil];
    }

The default sort order is the existing order of assets in collections including the "Camera Roll". Passing "nil" for options gets the default. You can reorder the images in the camera roll and this code will reflect the changes.


There is a super simple solution.

  1. Create a fetch result without any specific options.

    // ...
    
    let options = PHFetchOptions()
    fetchResult = PHAsset.fetchAssetsWithMediaType(.Image, options: options)
    
    if fetchResult.count > 0 {
      collectionView.reloadData()
    }
    
    // ...
    
  2. When you try to load a certain asset just use a reversed index.

    // ...
    
    let reversedIndex = fetchResult.count - indexPath.item - 1
    let asset = fetchResult[reversedIndex] as! PHAsset
    
    // ...
    

That way you'll get exactly the same flow order as in the Photo.app.

Hope you find it helpful. :]