使用PHAsset获取图片时避免重复

在iOS 8上,我想要获取存储在设备上的所有图片。 我的问题是,我得到他们,但有一些不止一次出席。 PHAsset属性(hidden,mediaSubtypes等)对于所有图片都是相同的,所以我不能排除PHAssetMediaSubtypePhotoHDR子types。 我find的唯一方法是不添加具有相同date的多个图片,但这是多个照片保存具有相同的创builddate时的问题。

有人知道我为什么得到这些副本,我能做些什么来避免它们?

这是我如何得到的图片:

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

您可以尝试使用Moments集合:

 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 } } 

自iOS 8.1以来, fetchAssetsWithMediaType:fetchAssetsWithOptions:方法的行为已经发生变化,并且不再包含从iTunes同步到设备的照片或存储在iCloud共享照片stream中的照片。

来源: 文档修订历史和PHAsset类参考 。

我有同样的问题,对我来说,重复的是在我的照片stream相册中的图像。 要解决此问题,我现在使用PHAssetCollection类中的FetchMoments方法,然后在提取结果中的每个时刻获取所有资源。 这样我得到所有的图像没有得到重复的图像。

如果有人find更好的解决scheme,请让我知道。

在传单上,这些资产是爆炸的一部分吗? (参考PHAsset.burstIdentifier等)如果是这样,你可以相应地进行调整。

您可以使用“PHImageRequestOptions”仅设置高质量图像!

 //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() } }) }) }