跟进@Majotron 的回答,我想按照照片应用程序中All Photos 相册的确切顺序获取所有用户的照片,并且只有图像资产。
这是我设置PHFetchOptions的方法:
let allPhotosOption = PHFetchOptions()
// Fetch only image asset
allPhotosOption.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.image.rawValue)
// Get All Photos album collection
let userLibrary = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .smartAlbumUserLibrary, options: nil).firstObject
allPhotosResult = PHAsset.fetchAssets(in: userLibrary!, options: allPhotosOption)
并使用 TableView/CollectionView 中的反向索引获取特定资产:
let asset = allPhotosResult.object(at: allPhotosResult.count - indexPath.item - 1)
如果您正在收听照片库的更改并相应地重新排列您的视图,请务必小心。当更改与删除相关时,使用更改前的获取结果获取反向索引,否则使用.fetchResultAfterChanges。
我是这样处理的:
func photoLibraryDidChange(_ changeInstance: PHChange) {
...
DispatchQueue.main.sync {
let originalCount = fetchResult.count
fetchResult = changes.fetchResultAfterChanges
if changes.hasIncrementalChanges {
guard let collectionView = self.collectionView else { return }
collectionView.performBatchUpdates({
if let removed = changes.removedIndexes, removed.count > 0 {
collectionView.deleteItems(at: removed.map({ IndexPath(item: originalCount - $0 - 1, section: 0) }))
}
if let inserted = changes.insertedIndexes, inserted.count > 0 {
collectionView.insertItems(at: inserted.map({ IndexPath(item: fetchResult.count - $0 - 1, section: 0) }))
}
})
// Avoid deleting and reloading same index path at one update
collectionView.performBatchUpdates({
if let changed = changes.changedIndexes, changed.count > 0 {
collectionView.reloadItems(at: changed.map({ IndexPath(item: fetchResult.count - $0 - 1, section: 0) }))
}
changes.enumerateMoves { fromIndex, toIndex in
collectionView.moveItem(at: IndexPath(item: fetchResult.count - $0 - 1, section: 0),
to: IndexPath(item: fetchResult.count - $0 - 1, section: 0))
}
})
} else {
collectionView!.reloadData()
}
...
}
}