gpt4 book ai didi

ios - 快速检测照片库中已删除的图像

转载 作者:行者123 更新时间:2023-11-28 13:58:45 26 4
gpt4 key购买 nike

我正在保存照片库图像的 PHAssets 的本地标识符并显示这些 Collection View 中的图像。我的问题是当我从照片库中删除图像时 然后我的应用程序崩溃,因为它无法获取已删除的 PHAsset 从照片库。这是我显示 Assets 的代码:

 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)。如果您使用 View Controller 作为观察者,您应该能够捕获 Collection View 中的所有更改,如下所示。下面的示例假设您有一个 Collection View 需要显示其图像的所有 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) 来捕获它们是否已在应用程序运行时被删除。不过,这在应用程序的 session 之间不起作用。

您可以创建一个相册并将您的应用使用的所有图像存储在该相册中,而不是存储本地标识符数组。然后您可以观察该相册的变化。

顺便说一句,您遇到崩溃的原因是您要求空数组的数组元素 [0]。您可以通过检查 PHAsset.fetchAssets() 调用的结果是否有大于零的计数来避免崩溃。

关于ios - 快速检测照片库中已删除的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53710602/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com