Ios 为什么使用照片框架的示例应用程序在每次更改后都使用stopCachingImagesForAllAssets?
所以我在这里看照片API和苹果的示例代码 在这里转换成swift 我已经将代码集成到我的项目中,以便在我拍照时collectionView能够成功地从库中更新自己。有一个怪癖:有时单元格是空白的,它似乎连接到stopCachingImagesForAllAssets,每当库在photoLibraryDidChange委托方法的末尾更新时,苹果都会调用它 我可以删除该行,它解决了问题,但肯定有一个原因,苹果公司把它放在第一位?我关心内存的使用Ios 为什么使用照片框架的示例应用程序在每次更改后都使用stopCachingImagesForAllAssets?,ios,swift,photosframework,Ios,Swift,Photosframework,所以我在这里看照片API和苹果的示例代码 在这里转换成swift 我已经将代码集成到我的项目中,以便在我拍照时collectionView能够成功地从库中更新自己。有一个怪癖:有时单元格是空白的,它似乎连接到stopCachingImagesForAllAssets,每当库在photoLibraryDidChange委托方法的末尾更新时,苹果都会调用它 我可以删除该行,它解决了问题,但肯定有一个原因,苹果公司把它放在第一位?我关心内存的使用 // MARK: - PHPhotoLibrary
// MARK: - PHPhotoLibraryChangeObserver
func photoLibraryDidChange(changeInstance: PHChange) {
// Check if there are changes to the assets we are showing.
guard let
assetsFetchResults = self.assetsFetchResults,
collectionChanges = changeInstance.changeDetailsForFetchResult(assetsFetchResults)
else {return}
/*
Change notifications may be made on a background queue. Re-dispatch to the
main queue before acting on the change as we'll be updating the UI.
*/
dispatch_async(dispatch_get_main_queue()) {
// Get the new fetch result.
self.assetsFetchResults = collectionChanges.fetchResultAfterChanges
let collectionView = self.pictureCollectionView!
if !collectionChanges.hasIncrementalChanges || collectionChanges.hasMoves {
// Reload the collection view if the incremental diffs are not available
collectionView.reloadData()
} else {
/*
Tell the collection view to animate insertions and deletions if we
have incremental diffs.
*/
collectionView.performBatchUpdates({
if let removedIndexes = collectionChanges.removedIndexes
where removedIndexes.count > 0 {
collectionView.deleteItemsAtIndexPaths(removedIndexes.aapl_indexPathsFromIndexesWithSection(0))
}
if let insertedIndexes = collectionChanges.insertedIndexes
where insertedIndexes.count > 0 {
collectionView.insertItemsAtIndexPaths(insertedIndexes.aapl_indexPathsFromIndexesWithSection(0))
}
if let changedIndexes = collectionChanges.changedIndexes
where changedIndexes.count > 0 {
collectionView.reloadItemsAtIndexPaths(changedIndexes.aapl_indexPathsFromIndexesWithSection(0))
}
}, completion: nil)
}
self.resetCachedAssets() //perhaps prevents memory warning but causes the empty cells
}
}
//MARK: - Asset Caching
private func resetCachedAssets() {
self.imageManager?.stopCachingImagesForAllAssets()
self.previousPreheatRect = CGRectZero
}
我也得到了同样的结果。 以下是为我解决问题的方法: 由于
performBatchUpdates
是异步的,因此可能在执行删除/插入/重新加载操作时执行resetCachedAssets
,甚至在这些操作之间执行
这听起来不太好。所以我移动了线路:
self.resetCachedAssets()
到dispatch\u async
块的第一行
我希望这对你也有帮助。哦,很有趣。嗯,考虑到您提到的PerformBatChUpdate是异步的,我们是否真的希望将self.resetCachedAssets()放在PerformBatChUpdate的完成处理程序中,而不是将其保留为零?然后我想它也需要在if语句的另一部分调用