PhotoKit:fetchAssetCollectionsWithLocalIdentifiers失败

Gre*_*gzo 5 ios photokit

以下代码通过3个简单的步骤突出了该问题:

1)获取时刻

2)缓存时刻localIdentifier

3)带有标识符的获取时刻:fail(在设备上,iOS 8.2)

- ( void )momentLocalIdTest
{
    PHFetchResult       * fetchResult;
    PHAssetCollection   * moment;
    NSString            * localIdentifier;

    fetchResult = [ PHAssetCollection fetchMomentsWithOptions: nil ];

    if( fetchResult.count == 0 )
        return;

    moment          = fetchResult.firstObject;
    localIdentifier = moment.localIdentifier;
    fetchResult     = [ PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers: @[ localIdentifier ] options: nil ];

    if( fetchResult.count == 0 )
        NSLog( @"AssetCollection with localIdentifier %@ not found!!!", localIdentifier );
}
Run Code Online (Sandbox Code Playgroud)

我误会了吗?看起来很简单...

任何帮助表示赞赏!

Olo*_*iar 0

我也遇到了同样的问题,无法弄清楚这段代码有什么问题。我认为这个 API 很简单而且很简单(至少从 8.0 到 8.4)

这是一个解决方法代码;您基本上必须从标识符重新生成 PHAssetCollection 实例

PHFetchOptions *options = [PHFetchOptions new];
options.predicate = [NSPredicate predicateWithFormat:@"localIdentifier = %@", identifier];

PHAssetCollection *collection = [[PHAssetCollection fetchMomentsWithOptions:options] firstObject];
PHFetchResult *results = [PHAsset fetchAssetsInAssetCollection:collection options:nil];
Run Code Online (Sandbox Code Playgroud)