Search code examples
iosobjective-cios8photos

Avoiding duplicates when getting pictures with PHAsset


On iOS 8, I want to get all pictures stored on the device. My problem is that I do get them, but some are present more than once. The PHAsset properties (hidden, mediaSubtypes, etc.) are the same for all pictures, so I can't for example rule out the PHAssetMediaSubtypePhotoHDR subtypes. The only way I found is not adding multiple pictures with the same date, but this is a problem when multiple photos were saved with the same creation date.

Does anybody know why I get these duplicates and what I can do to avoid them?

This is how I get the pictures:

    PHFetchOptions *fetchOptions = [PHFetchOptions new];
    fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES],];
    PHFetchResult *phAssets = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions];

Solution

  • You can try to use Moments Collections:

    PHFetchResult * moments = [PHAssetCollection fetchMomentsWithOptions:nil];            
    for (PHAssetCollection * moment in moments) {
        PHFetchResult * assetsFetchResults = [PHAsset fetchAssetsInAssetCollection:moment options:nil];
        for (PHAsset * asset in assetsFetchResults) {
            //Do something with asset, for example add them to array
        }
    }