1器紧、在viewDidLoad注冊(cè)通知
PHPhotoLibrary.sharedPhotoLibrary().registerChangeObserver(self)
2、在函數(shù)photoLibraryDidChange下添加如下內(nèi)容
dispatch_async(dispatch_get_main_queue()) {
let collectionChanges = changeInstance.changeDetailsForFetchResult(self.allResults)
if collectionChanges != nil {
// 是否變化
if collectionChanges!.hasIncrementalChanges {
// 監(jiān)聽(tīng)增加或者刪除
if (collectionChanges!.insertedObjects.count > 0) || (collectionChanges!.removedObjects.count > 0) {
self.allResults = collectionChanges?.fetchResultAfterChanges
self.photosA = self.allResults
self.imageCollectionView.reloadData()
}
} else {
// 首次的時(shí)候走這里
if collectionChanges!.fetchResultBeforeChanges.count == 0 {
self.allResults = collectionChanges?.fetchResultAfterChanges
self.photosA = self.allResults
self.imageCollectionView.reloadData()
}
}
}
}
3楼眷、在cellForItemAtIndexPath里顯示自定義的CollectionView
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cellID", forIndexPath: indexPath) as! MyCollectionViewCell
PHCachingImageManager.defaultManager().requestImageForAsset(PCommon.photosA![PCommon.photosA!.count - indexPath.row - 1] as! PHAsset, targetSize: CGSizeZero, contentMode: .AspectFit, options: nil) { (result: UIImage?, dictionry: Dictionary?) in
cell.imageView.image = result ?? UIImage.init(named: "默認(rèn)的圖片")
}
至此铲汪,照片就很方便的展示在自定義的CollectionView中。