Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting photos exactly like native photoapp using PhotoKit

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?

like image 620
knutigro Avatar asked Jul 29 '15 09:07

knutigro


3 Answers

swift 3

let fetchOptions = PHFetchOptions()
    let sortOrder = [NSSortDescriptor(key: "creationDate", ascending: false)]
    fetchOptions.sortDescriptors = sortOrder
like image 81
Felecia Genet Avatar answered Nov 10 '22 00:11

Felecia Genet


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.

like image 8
knutigro Avatar answered Nov 09 '22 23:11

knutigro


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
}
like image 5
Jawwad Avatar answered Nov 10 '22 00:11

Jawwad