ios - 快速检测从照片库中删除的图像
问题描述
我正在保存照片库图像的 PHAsset 的本地标识符,并在集合视图中显示这些图像。我的问题是,当我从照片库中删除图像时,我的应用程序崩溃,因为它无法获取已从照片库中删除的 PHAsset。这是我显示资产的代码:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = photoCollectionView.dequeueReusableCell(withReuseIdentifier: "imageShowCell", for: indexPath) as! imageShowCell
let image = photoArray.object(at: indexPath.item) as! Photos
let imageManager = PHImageManager()
let asset = PHAsset.fetchAssets(withLocalIdentifiers: [image.pic_name!], options: nil)[0]
let scale = UIScreen.main.scale
let size = CGSize(width: 50.0 * scale, height: 50.0 * scale)
imageManager.requestImage(for: asset, targetSize: size, contentMode: .aspectFill, options: nil) { (image, _) in
cell.imageView.image = image
}
return cell
}
解决方案
您需要在照片库中注册变更观察者。然后,您会在照片被删除、插入、更改或移动时收到通知。观察者需要继承自PHPhotoLibraryChangeObserver
. 然后您需要实现该功能photoLibraryDidChange(_ changeInstance: PHChange)
。如果您使用视图控制器作为观察者,您应该能够捕获集合视图中的所有更改,如下所示。下面的示例假设您有一个集合视图所需的所有 phAssets 数组,以显示其随时可用的图像
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 对象中,您可以使用PHChange.changeDetails(for object: PHObject)
它们中的每一个来捕获它们是否在应用程序运行时被删除。但是,这在应用程序的会话之间不起作用。
您可以创建一个相册并将您的应用程序使用的所有图像存储在该相册中,而不是存储一组本地标识符。然后您可以查看该专辑的更改。
顺便说一句,您遇到崩溃的原因是您要求空数组的数组元素 [0]。您可以通过检查PHAsset.fetchAssets()
调用结果的计数是否大于零来避免崩溃。
推荐阅读
- mongodb - 如何过滤 Mongodb $lookup 结果以仅获取匹配的嵌套对象?
- java - 防止 VSCode 为 Java 将流方法放在彼此之后
- java - 从 Java ProcessBuilder 执行幕后 bash 命令
- reactjs - 如果在 beforeEach 中进行浅渲染,是否会影响开玩笑的测试性能
- javascript - 如何定位我的内存泄漏源?
- c# - 使用 Microsoft.CongnitiveServices.Speech 的 Visual Studio C# 项目在带有 Raspbian 的 Raspberry pi 4 上不起作用,有人可以帮我吗?
- queue - AnyLogic 表示排队等待时间
- excel - 将单元格与分隔符组合在一起 如何在 Excel 中实现这一点?
- mongodb - DigiCert 证书不适用于 MongoDB
- python - openpyxl - 无法遍历 excel 行