Unikanie duplikatów podczas robienia zdjęć za pomocą PHAsset

Na iOS 8, chcę, aby wszystkie zdjęcia przechowywane na urządzeniu. Mój problem polega na tym, że je dostaję, ale niektóre są obecne więcej niż raz. Właściwości PHAsset (hidden, mediasubtyp, itp.) są takie same dla wszystkich obrazków, więc nie mogę na przykład wykluczyć podtypów PHAssetMediaSubtypePhotoHDR. Jedynym sposobem, który znalazłem, jest nie dodawanie wielu zdjęć z tą samą datą, ale jest to problem, gdy wiele zdjęć zostało zapisanych z tą samą datą utworzenia.

Czy ktoś wie dlaczego dostaję te duplikaty i co można zrobić, aby ich uniknąć?

Tak robię zdjęcia:

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

5 answers

Możesz spróbować użyć kolekcji momentów:

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
Author: Ponf,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-09-23 13:41:07

Od wersji iOS 8.1 zachowanie metod fetchAssetsWithMediaType: i fetchAssetsWithOptions: zmieniło się i nie zawierają już Zdjęć zsynchronizowanych z urządzeniem z iTunes ani zdjęć przechowywanych w udostępnionym strumieniu zdjęć iCloud.

Source: Document Revision History and Phasset Class Reference.

 6
Author: Rufel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-10-22 16:41:11

Miałem ten sam problem, a dla mnie duplikaty były obrazkami, które znajdowały się w albumie my photostream. Aby obejść ten problem, używam teraz metody FetchMoments z klasy PHAssetCollection, a następnie pobieram wszystkie zasoby dla każdej chwili w wyniku fetch. W ten sposób otrzymuję wszystkie obrazy bez powtarzania się obrazów.

Jeśli ktoś znajdzie lepsze rozwiązanie, proszę dać mi znać.

 3
Author: Nuno,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-09-23 13:18:36

Na ulotce, czy te aktywa są częścią wybuchu? (por. PHAsset.burstIdentifier, itd.) Jeśli tak, można dostosować odpowiednio.

 0
Author: Clay Bridges,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-01-22 13:47:14

Możesz użyć "PHImageRequestOptions", aby skonfigurować tylko obrazy wysokiej jakości na przykład!

//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
Author: Almeida Cavalcante,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-06 19:12:52