tilltue / TLPhotoPicker

📷 multiple phassets picker for iOS lib. like a facebook
MIT License
1.88k stars 332 forks source link

Same sorting order as Photos.app #267

Open ppoh71 opened 4 years ago

ppoh71 commented 4 years ago

Hi, i have some problems to get the same sort order the actual photo app has.

When i upload an image to my photos e.g via airdrop or download from the internet, this photo appears on top in my photo.app. Even if the creationDate of this photo is older or from last year, etc. But in TLPhotoPicker the sorting is defined by creation date, which results in a different sorting, because of the older creationDates in this example.

I tried using PHFetchOptions:

  1. sortDescriptors: "creationDate" let fetchOptions = PHFetchOptions() fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]

This is the default and the sorting is different from photos.app like described

  1. sortDescriptors: "modificationDate" let fetchOptions = PHFetchOptions() fetchOptions.sortDescriptors = [NSSortDescriptor(key: "modificationDate", ascending: false)]

Looks close to the sorting of the photos.app but still the sorting is slightly different to the photos.app

  1. No sortDescriptors at all

let fetchOptions = PHFetchOptions()

When i use just fetchOption without any sortDescriptors, the result is sorting is correct, but in the wrong order. --> Oldest photos first. This could be some kind of workaround, when it would be possible to invert the result array...

Did i miss something here or is there no way to get the default sorting from the photos app ?

p.s.: best photos library i have seen :-) best regards, peter

sanketfirodiya commented 4 years ago

+1 we would like a solution to this for our project as well.

paulstampfer commented 4 years ago

Hi, @ppoh71 like you correctly stated, the current default sort order is by creation date which is the same than the camera roll of the photos app, but not the recents of the photos app.

That means if you add a photo from somewhere, which was taken years ago (i.e. creationDate is years ago), it will show under the camera roll in the year it was taken (far back, and the user would have to scroll all the way back), while in the recents you see it right at last, with the date when it was added.

Also on Stackoverflow the only solution mentioned for achieving this, is by using no sortDescriptors, like you wrote: let fetchOptions = PHFetchOptions()

The only way how to deal with the reverse order of the fetchResults is to use a reverse index, like described here: https://stackoverflow.com/a/38364910

On the first view, I guess this would need to be implemented in TLAssetsCollection.swift -> func getTLAsset(at indexPath: IndexPath) -> TLPHAsset?

+1 for a solution where we could switch between the sorting by creationDate and that recents sorting with a reversed index.

If you want a quick fix, you could use that hack in TLAssetsCollection.swift (just replace the func getTLAsset(at indexPath: IndexPath) -> TLPHAsset?):

func getTLAsset(at indexPath: IndexPath) -> TLPHAsset? {
    let isCameraRow = self.useCameraButton && indexPath.section == 0 && indexPath.row == 0
    if isCameraRow {
        return nil
    }
    if let sections = self.sections {
        let index = indexPath.row - ((self.useCameraButton && indexPath.section == 0) ? 1 : 0)
        let result = sections[safe: indexPath.section]
        return result?.assets[safe: index]
    }else {
        var index = indexPath.row
        index = index - (self.useCameraButton ? 1 : 0)
        guard let result = self.fetchResult else { return nil }
        let reversedIndex = result.count - index - 1
        guard reversedIndex < result.count else { return nil }
        return TLPHAsset(asset: result.object(at: max(reversedIndex,0)))
    }
}
christinachanhk commented 8 months ago

+1 for a solution to switch between the sorting by creationDate and that recents sorting with a reversed index.