9

Is there any way to preserve the list order a user has arranged all of their albums in the Photos app? I've been trying to fix this all day, and I can't seem to figure out what I'm missing to preserve the order for displaying in my app.

I would like to preserve the order because a user can rearrange their albums in the Photos app, so I want my UI to match the list order.

Code I'm using to set up the list:

PHFetchResult *smartAlbumFetchResult = [PHAssetCollection fetchAssetCollectionsWithType:
PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAny options:nil];

PHFetchResult *albumFetchResult = [PHAssetCollection fetchAssetCollectionsWithType:
PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:nil];

NSArray *collectionFetches = @[smartAlbumFetchResult, albumFetchResult];

NSMutableArray *albumArray = [NSMutableArray new];

for (PHFetchResult *newFetch in collectionFetches)
{
    for (PHAssetCollection *sub in newFetch)
    {
        [albumArray addObject:sub];
         // Other setup omitted
    }
}
klcjr89
  • 5,862
  • 10
  • 58
  • 91

2 Answers2

5

Sorry, but I don't think it's possible actually (I hope in some upgrades).
In fact, it is possible to use PHFetchOptions to sort asset collections, but PhotoKit supports only a restricted set of keys for the predicate and sortDescriptors properties (see PHFetchOptions) and between these I can't see a key for the the list order a user has arranged in the Photos app.

asclepix
  • 7,971
  • 3
  • 31
  • 41
-1

Can't you make a property, in the PHAssetCollection class, which takes the order into account? That way you can show them based on the property.

A very simple example, if the class contains a number from 1 - n, based on that number you know which class/photo to show?(Or sort them based on that property and you can just loop it after the sort.)

@Edit The simple example:

In the class PHAssetCollection in the .h file you declare a property.

@property (retain, nonatomic) NSInteger id;

In the class PHAssetCollection in the .m file you can fill him in the initializer method. If you want to use him in the .m file you have to synthesize him. You can off course choose to fill how ever and when ever you want, that depends on your needs. I can imagine that they get a standard id and when a user drags a photo it sets a new id onto it.

You can sort your list based on the following, implement the compare method and call the compare functionality:

//Implement
- (NSComparisonResult)compare:(PHAssetCollection *)otherPhoto{
    return [self.id compare:otherPhoto.id];
}

//Call method
NSMutableArray *sortedArray;
sortedArray = [sub sortedArrayUsingSelector:@selector(compare:)];

There are many ways to accomplish it. Another one more complicated is to keep track of the order in the background and not change the id every time the order gets changed. When the ApplicationWillTerminate or ApplicationDidEnterBackground is invoked, then you change the id of the photos based on the order of photos. That way you will only need to change the id one time and that will save you some performance.

Revils
  • 1,478
  • 1
  • 14
  • 31
  • Your code example doesn't really make sense for the question I posted. If you use the Photos app in iOS and change the album order, the PHFetchResults don't give you back the correct order. Also you can't modify PHAssetCollection. – klcjr89 Jun 18 '15 at 16:07
  • Just gave some guidelines and trying to explain the idea, not going to spell it out for you.. – Revils Jun 18 '15 at 18:19
  • This is a large bounty, you didn't even research the issue at hand – klcjr89 Jun 18 '15 at 18:35
  • I am not in for the bounty,.. just trying to give some suggestions. – Revils Jun 18 '15 at 19:16
  • @Silver2009 aviatorken89 asks how to shows in his app the order the user has selected in the iOS Photos app, not the order the user sets in his app. – asclepix Jun 19 '15 at 21:21