ios PhotoKit:使用本地标识符获取资产集合失败

xdnvmnnf  于 2023-03-20  发布在  iOS
关注(0)|答案(2)|浏览(104)

下面的代码通过3个简单的步骤突出显示问题:
1)提取时刻
2)缓存时刻本地标识符
3)获取带有标识符的时刻:失败(在设备上,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 );
}

我是不是误解了什么?看起来很简单...
任何帮助感激不尽!

ekqde3dh

ekqde3dh1#

我也遇到了同样的问题,找不出这段代码有什么问题。我认为这段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];
e0bqpujr

e0bqpujr2#

- (void)ph_example{
    NSError *error;
    __block NSString *sid = nil;
    NSString *title = @"foldra-moldra";

    dispatch_block_t block_1 = ^{
        PHAssetCollectionChangeRequest *request = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:title];
        sid = request.placeholderForCreatedAssetCollection.localIdentifier;
    };

    dispatch_block_t block_2 = ^{
        PHFetchResult <PHAssetCollection *> *result =
        [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[sid] options:nil];
        PHAssetCollection *collection = result.firstObject;
        printf("collection: %p\r", collection);
    };

#define PH_SUCCESS_INSERT_AND_FETCH 1
#if PH_SUCCESS_INSERT_AND_FETCH
    [PHPhotoLibrary.sharedPhotoLibrary performChangesAndWait:block_1 error:&error];
    //
    // HERE: Need a flow break of -[PHPhotoLibrary performChanges:] or -[PHPhotoLibrary performChangesAndWait:]...
    // Fetch only after insert changes did applied
    // block{Create} => block{Fetch} => Found
    //
    [PHPhotoLibrary.sharedPhotoLibrary performChangesAndWait:block_2 error:&error];
#else
    // block{Create => Fetch} => Not Found
    [PHPhotoLibrary.sharedPhotoLibrary performChangesAndWait:^{
        block_1();
        block_2();
    } error:&error];
#endif
}

相关问题