Search code examples
photokitphassetphphotolibraryphfetchoptions

PhotoKit: includeAllBurstAssets in PHFetchOptions not working


I am trying to use the includeAllBurstAssets of PHFetchOptions. In my camera roll, there are about 5 burst assets (each with about 10 photos).

To enumerate the assets in the Camera Roll I'm doing the following:

PHFetchOptions *fetchOptions = [PHFetchOptions new];

fetchOptions.includeAllBurstAssets = YES;

PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:fetchOptions];
PHFetchResult *assetFetch = [PHAsset fetchAssetsInAssetCollection:fetchResult[0] options:fetchOptions];

NSLog(@"Found assets %lu",(unsigned long)assetFetch.count);

No matter, if I set the includeAllBurstAssets property to NO or YES, I get the exact same count of assets. I expected the number to be higher, if includeAllBurstAssets is set to YES. Is this a bug or I am interpreting the includeAllBurstAssets in a wrong way.


Solution

  • There is a special method to query for all images of a burst sequence.

    [PHAsset fetchAssetsWithBurstIdentifier:options:]
    

    In your case you need to iterate over your assetFetch object and check if the PHAsset represents a burst.

    PHAsset defines the property BOOL representsBurst

    If that returns YES, fetch all assets for that burst sequence.

    Here is a code snippet that might help to understand:

    if (asset.representsBurst) {
        PHFetchOptions *fetchOptions = [PHFetchOptions new];
        fetchOptions.includeAllBurstAssets = YES;
        PHFetchResult *burstSequence = [PHAsset fetchAssetsWithBurstIdentifier:asset.burstIdentifier options:fetchOptions];
        PHAsset *preferredAsset = nil;
        for (PHAsset *asset_ in burstSequence) {
            if (PHAssetBurstSelectionTypeUserPick == asset.burstSelectionTypes || PHAssetBurstSelectionTypeAutoPick == asset.burstSelectionTypes) {
                asset = preferredAsset = asset_;
            }
        }
        if (!preferredAsset) {
            asset = burstSequence.firstObject;
        }
    } 
    

    As you can see, the burstSelectionTypes are not always set resp. are sometimes PHAssetBurstSelectionTypeNone for all assets of a burst sequence.