您需要向照片库注册变更观察者。然后,您会在照片被删除、插入、更改或移动时收到通知。观察者需要继承自PHPhotoLibraryChangeObserver。然后你需要实现函数photoLibraryDidChange(_ changeInstance: PHChange)。如果您使用视图控制器作为观察者,您应该能够捕获集合视图中的所有更改,如下所示。下面的示例假设您拥有一个集合视图所需的所有 phAsset 数组,以显示其随时可用的图像
class MyViewController : UIViewController, PHPhotoLibraryChangeObserver {
func viewDidLoad() {
...
PHPhotoLibrary.shared().register(self)
...
}
func photoLibraryDidChange(_ changeInstance: PHChange) {
// Change notifications may be made on a background queue.
// Re-dispatch to the main queue to update the UI.
// Check for changes to the displayed album itself
// (its existence and metadata, not its member self).
guard let photos = photos else {return}
// Check for changes to the list of assets (insertions, deletions, moves, or updates).
if let changes = changeInstance.changeDetails(for: photos) {
// Keep the new fetch result for future use.
photos = changes.fetchResultAfterChanges
if changes.hasIncrementalChanges {
// If there are incremental diffs, animate them in the collection view.
self.collectionView.performBatchUpdates({
// For indexes to make sense, updates must be in this order:
// delete, insert, reload, move
if let removed = changes.removedIndexes, removed.count > 0 {
print("photoLibraryDidChange: Delete at \(removed.map { IndexPath(item: $0, section:0) })")
self.collectionView.deleteItems(at: removed.map { IndexPath(item: $0, section:0) })
}
if let inserted = changes.insertedIndexes, inserted.count > 0 {
print("photoLibraryDidChange: Insert at \(inserted.map { IndexPath(item: $0, section:0) })")
self.collectionView.insertItems(at: inserted.map { IndexPath(item: $0, section:0) })
}
if var changed = changes.changedIndexes, changed.count > 0 {
print("photoLibraryDidChange: Reload at \(changed.map { IndexPath(item: $0, section:0) })")
// subtract removed indices
if let removed = changes.removedIndexes {
changed.subtract(removed)
}
self.collectionView.reloadItems(at: changed.map { IndexPath(item: $0, section:0) })
}
changes.enumerateMoves { fromIndex, toIndex in
print("photoLibraryDidChange: Move at \(IndexPath(item: fromIndex, section:0)) to \(IndexPath(item: toIndex, section:0 ))")
self.collectionView.moveItem(at: IndexPath(item: fromIndex, section: 0), to: IndexPath(item: toIndex, section: 0))
}
})
} else {
// Reload the collection view if incremental diffs are not available.
...
}
}
}
var photos : PHFetchResult<PHAsset>?
weak var collectionView : UICollectionView!
}
目前您正在临时创建 PHAsset。您需要某种形式的永久 PHObject 才能使上述功能发挥作用。如果您将单个 PHAsset 存储在您的 photoArray 对象中,您可以在每个 PHAsset 上使用PHChange.changeDetails(for object: PHObject) 来捕获它们是否在应用程序运行时被删除。但是,这在应用程序的会话之间不起作用。
您可以创建一个相册并将您的应用使用的所有图像存储在该相册中,而不是存储一组本地标识符。然后您可以查看该专辑的更改。
顺便说一句,您遇到崩溃的原因是您要求空数组的数组元素 [0]。您可以通过检查 PHAsset.fetchAssets() 调用的结果是否计数大于零来避免崩溃。