19

I am trying to reproduce the native iOS8 photopicker using Photos framework. I got a problem with the sorting.

Lets say I do following:

  1. I edit a photo in Camera+ app and save it back to gallery.
  2. I favourite another photo.

In the native photopicker:

  • All photos (even the one I favourited) will be sorted by creationDate
  • The photo I edited and saved will be down at the bottom since it was the last one I changed. The original will be at its original place according to its creation date.

In my app I do following:

PHFetchOptions *options = [PHFetchOptions new];
options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
options.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld", PHAssetMediaTypeImage];
PHFetchResult *fetchResult = [PHAsset fetchAssetsInAssetCollection:self.assetCollection options:options];

I sort in creationDate and get following result:

  • All photos (even the one I favourited) will be sorted by creationDate
  • The photo I edited will be just after at it's original according to it's original creation date.

Then I change my query so instead of sorting on creationDate, I sort on modificationDate. I get following result:

  • All photos are in general in order almost same as creationDate
  • The photo I edited will be at the bottom. This is what I want and like in the native app.
  • The favourited photo will also be at the bottom :-(

So it seeme Apple change modificationDate on favourite action and probably also on other kind of actions and this mess up the sorting of the photos.

How is it possible to get exactly the sorting Apple use in its native app? Maybe some clever use of NSSortDescriptor?

6 Answers 6

9

swift 3

let fetchOptions = PHFetchOptions()
    let sortOrder = [NSSortDescriptor(key: "creationDate", ascending: false)]
    fetchOptions.sortDescriptors = sortOrder
2
  • I used this method for modificationDate but doesn't work and still get creationDate in photos why? please help me Jul 17, 2017 at 6:02
  • 3
    That doesn't give the same result as the native Photos app, which is kinda "added date" or so. For me, only the solutions, not using a sort descriptor at all led to the same result as the Photos app.
    – d4Rk
    Dec 11, 2019 at 12:12
8

I just found out that to copy the exact behaviour of the native photopicker the sollution was to remove my custom sortDescriptiorand just use the PHFetchResult with default behaviour. It seeme so obvious now after discovering that.

5
  • 1
    Having tested this on several devices I see cases where the order is not correct. It looks like as of iOS 9 there is no way to match the native photos app exactly.
    – jjxtra
    Feb 9, 2016 at 16:30
  • @jjxtra Can you tell what are these incorrect cases?
    – kolyuchiy
    Feb 10, 2016 at 7:51
  • 1
    On my wifes phone she has about 20 albums. Both the smart albums and regular albums come back in a different order, although close to matching, the native photos app.
    – jjxtra
    Feb 10, 2016 at 16:34
  • This has stopped working in ios12. Using this the images in iCloud always appear on top, followed by camera roll imaegs.
    – manish
    Sep 13, 2018 at 12:59
  • 1
    Nope, still broken.
    – manish
    Nov 2, 2018 at 17:05
5

As @knutigro mentioned the solution is to use PHFetchResult with the default options i.e. by passing in nil for the options parameter:

var fetchResult: PHFetchResult!

override func viewDidLoad() {
    super.viewDidLoad()

    fetchResult = PHAsset.fetchAssetsWithMediaType(.Image, options: nil)
}

But the next step is reversing the sort results so that the most recent image is first. There isn't an easy way to reverse the results in PHFetchResult so I used the following method:

func assetAtIndex(index: Int) -> PHAsset {
    // Return results in reverse order
    return fetchResult[fetchResult.count - index - 1] as! PHAsset
}
3
  • You think it's safe to say it'll stay reversed? Plus, why does Apple return the results reversed from the first place?
    – Roi Mulia
    Dec 31, 2017 at 12:51
  • @Husam - In my opinion, I don't think its likely that they will change it since if they did, it would break a lot of apps, but of course there is no guarantee.
    – Jawwad
    Jan 5, 2018 at 12:45
  • @Jawwad you mentioned me !
    – Husam
    Jan 5, 2018 at 12:48
2

You may try using two sort descriptors :

NSSortDescriptor sortDesc1 = [NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]]; 

NSSortDescriptor sortDesc2 = [NSSortDescriptor sortDescriptorWithKey:@"modificationDate" ascending:NO]];     

options.sortDescriptors = @[sortDesc1, sortDesc2];

Haven't tested, just thought it might work.

3
  • 2
    Yes, thanks, its probably some custom NSSortDescriptor but just by adding two desciptors, like you did here doesn't work in this case. I guess the second one will only apply if the first one is equal when two objects are compared. The dates are never equal on the photos.
    – knutigro
    Jul 29, 2015 at 10:00
  • 2
    Does not work, and photos download from the web which have creation dates years earlier show up in the wrong spot.
    – jjxtra
    Feb 9, 2016 at 16:31
  • @jjxtra did u find any solution for this yet? Oct 13, 2020 at 6:03
0

Dont ask me why and how, but when I set that sort description. It works like Photos application (Recents).

        [options setSortDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"burstIdentifier" ascending:NO]]];

Try it.

0
0

using NSSortDescriptor(key: "mediaType", ascending: false) works for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.