Avoiding Duplicate Image Acquisition Using PHASset

In iOS 8, I want to get all the pictures stored on the device. My problem is that I receive them, but some of them are present more than once. PHAsset properties (hidden, mediaSubtypes, etc.) are the same for all images, so I cannot, for example, exclude subtypes of PHAssetMediaSubtypePhotoHDR . The only way I found is not to add multiple photos with the same date, but this is a problem when multiple photos were saved with the same creation date.

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

This is how I get the photos:

  PHFetchOptions *fetchOptions = [PHFetchOptions new]; fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES],]; PHFetchResult *phAssets = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions]; 
+8
ios objective-c ios8 photos
Sep 17 '14 at 5:36 on
source share
5 answers

You can 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 } } 
+5
Sep 23 '14 at 13:41
source share

Starting with iOS 8.1, the behavior of the fetchAssetsWithMediaType: and fetchAssetsWithOptions: methods has changed and they no longer include photos synced to the device from iTunes or photos stored in the iCloud shared photo stream.

Source: Document Change History and PHAsset Class Link .

+6
Oct 22 '14 at 16:41
source share

I had the same issue and for me the duplicates were the images that were in my Photostream album. To get around this problem, I now use the FetchMoments method from the PHAssetCollection class, and then I retrieve all the assets for each moment as a result of the selection. This way I get all the images without duplicate images.

If someone finds a better solution, let me know.

+3
Sep 23 '14 at 13:18
source share

Are these assets part of the package on the flyer? (see PHAsset.burstIdentifier , etc.). If so, you can customize accordingly.

0
Dec 02 '14 at 17:52
source share

you can use "PHImageRequestOptions" to configure only high-quality images, for example!

 //Setting up the deliveryMode in PHImageRequestOptions() fileprivate func imageRequestOptions() -> PHImageRequestOptions { let requestOption = PHImageRequestOptions() requestOption.deliveryMode = .highQualityFormat return requestOption } fileprivate func fetchImages(){ let fetchOptions = assetsFetchOptions() //get fetchOptions only. Don`t worry let allPhotos = PHAsset.fetchAssets(with: .image, options: fetchOptions) allPhotos.enumerateObjects({ (asset, index, stop) in print(asset) let imageManager = PHImageManager.default() let targetSize = CGSize(width: 200, height: 200) //This function uses the "imageRequestOptions()" function to set up the "options:" field in this .requestImage() function. imageManager.requestImage(for: asset, targetSize: targetSize, contentMode: .aspectFit, options: self.imageRequestOptions(), resultHandler: { (image, info) in if let image = image { self.images.append(image) self.assets.append(asset) if self.selectedImage == nil { self.selectedImage = image } } if index == allPhotos.count - 1 { self.collectionView?.reloadData() } }) }) } 
0
Aug 6 '17 at 19:12
source share



All Articles