Photokit 保留用户相册列表顺序

Photokit preserve user album list order

有什么方法可以保留用户在“照片”应用中排列所有相册的列表顺序?我一整天都在尝试解决这个问题,但我似乎无法弄清楚我缺少什么来保留在我的应用程序中显示的顺序。

我想保留顺序,因为用户可以在照片应用中重新排列他们的相册,所以我希望我的 UI 与列表顺序相匹配。

我用来设置列表的代码:

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
    }
}

你不能在 PHAssetCollection class 中制作一个 属性,它考虑了顺序吗?这样你就可以根据 属性.

显示它们

一个非常简单的例子,如果 class 包含一个从 1 - n 的数字,根据这个数字你知道要显示哪个 class/photo 吗?(或者根据 [=31] 对它们进行排序=],你可以在排序后循环它。)

@编辑 简单的例子:

在 .h 文件的 class PHAssetCollection 中声明一个 属性.

@property (retain, nonatomic) NSInteger id;

.m文件中的classPHAssetCollection中你可以在initializer方法中填入他。如果你想在 .m 文件中使用他,你必须合成他。您当然可以选择何时何地填充,这取决于您的需要。我可以想象他们得到一个标准 ID,当用户拖动照片时,它会在上面设置一个新 ID。

您可以根据以下内容对列表进行排序,实现比较方法并调用比较功能:

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

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

有很多方法可以实现。另一个更复杂的是在后台跟踪订单,而不是每次更改订单时都更改 id。当调用 ApplicationWillTerminate 或 ApplicationDidEnterBackground 时,您可以根据照片的顺序更改照片的 ID。这样一来,您只需要更改一次 ID 即可节省一些性能。

抱歉,但我认为实际上不可能(我希望进行一些升级)。
事实上,可以使用 PHFetchOptions 对资产集合进行排序,但 PhotoKit 仅支持 predicate 和 sortDescriptors 属性的一组受限键(参见 PHFetchOptions),在这些之间我看不到用于用户在照片应用中排列的列表顺序。