[英]detect deleted image from photo gallery in swift
我要保存PHAssets圖片庫圖像的本地標識符,並在集合視圖中顯示這些圖像。 我的問題是,當我從照片庫中刪除圖像時,我的應用程序崩潰,因為它無法獲取從照片庫中刪除的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)
。 如果將視圖控制器用作觀察者,則應該能夠按以下方式捕獲集合視圖中的所有更改。 下面的示例假定您有一個集合,其中包含所有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對象中,則可以在每個PHChange.changeDetails(for object: PHObject)
上使用PHChange.changeDetails(for object: PHObject)
,以捕獲應用程序運行時是否已將其刪除。 不過,這在應用程序的會話之間將無法正常工作。
您可以創建相冊並將應用程序使用的所有圖像存儲在該相冊中,而不是存儲本地標識符數組。 然后,您可以查看對該相冊的更改。
另外,導致崩潰的原因是您要查詢空數組的數組元素[0]。 您可以通過檢查PHAsset.fetchAssets()
調用的結果計數是否大於零來避免崩潰。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.