Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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];
like image 902
olivier Avatar asked Sep 17 '14 05:09

olivier


2 Answers

Since iOS 8.1, the behavior of the fetchAssetsWithMediaType: and fetchAssetsWithOptions: methods has changed, and they no longer include photos synchronized to the device from iTunes or photos stored in an iCloud Shared Photo Stream.

Source: Document Revision History and PHAsset Class Reference.

like image 179
Rufel Avatar answered Oct 21 '22 17:10

Rufel


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
    }
}
like image 33
Ponf Avatar answered Oct 21 '22 16:10

Ponf