我已经为我的应用程序使用了照片的集合视图。 我很容易从集合视图中删除项目,但它不会立即从集合视图中删除项目。如果我转到另一个视图控制器并返回到集合视图控制器,那么它将更新并从集合视图中删除已删除的项目。我希望在删除操作发生时立即更新集合视图。我怎么解决这个问题?提前致谢。 我的快速代码如下:
import UIKit
import Photos
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate{
var assetCollection: PHAssetCollection = PHAssetCollection()
var photosAsset: PHFetchResult!
var assetThumbnailSize:CGSize!
var selectedIndexPath = [NSIndexPath]()
@IBAction func deleteButton(sender: UIButton) {
if selectedIndexPath.count != 0{
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
//Delete Photo
if let request = PHAssetCollectionChangeRequest(forAssetCollection: self.assetCollection){
for a in self.selectedIndexPath{
request.removeAssets([self.photosAsset[a.item]])
}
}
}, completionHandler: nil)}
self.collectionView.reloadData()
}
这是我的viewWillAppear方法:
override func viewWillAppear(animated: Bool) {
// Get size of the collectionView cell for thumbnail image
if let layout = self.collectionView.collectionViewLayout as? UICollectionViewFlowLayout{
let cellSize = layout.itemSize
self.assetThumbnailSize = CGSizeMake(cellSize.width, cellSize.height)
}
self.photosAsset = PHAsset.fetchAssetsInAssetCollection(self.assetCollection, options: nil)
if let photoCnt = self.photosAsset?.count{
if(photoCnt == 0){
self.noPhotosLabel.hidden = false
}else{
self.noPhotosLabel.hidden = true
}
}
self.collectionView.reloadData()
}
答案 0 :(得分:0)
您需要在完成处理程序中添加重载方法。
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
//Delete Photo
if let request = PHAssetCollectionChangeRequest(forAssetCollection: self.assetCollection){
for a in self.selectedIndexPath{
request.removeAssets([self.photosAsset[a.item]])
}
}
}, completionHandler: { _ in
self.photosAsset = PHAsset.fetchAssetsInAssetCollection(self.assetCollection, options: nil)
dispatch_async(dispatch_get_main_queue(), { self.collectionView.reloadData() })
})