目前,我有这段代码:
import Photos
public class AssetService : NSObject, PHPhotoLibraryChangeObserver {
public override init() {
super.init()
PHPhotoLibrary.sharedPhotoLibrary().registerChangeObserver(self)
}
deinit {
PHPhotoLibrary.sharedPhotoLibrary().unregisterChangeObserver(self)
}
public func photoLibraryDidChange(changeInstance: PHChange) {
dispatch_async(dispatch_get_main_queue(), {
})
}
}
如果我想检查相机胶卷中的更新,我可以在 photoLibraryDidChange
函数中做什么。我在开始时将所有图像/视频提取到 PHFetchResult 中,现在我想监视更改。
我始终以全屏方式在 UIView 中显示图像/视频,并使用 UIPageControl 滚动内容。将不胜感激任何帮助
最佳答案
var allPhotos: PHFetchResult<PHAsset>!
var smartAlbums: PHFetchResult<PHAssetCollection>!
var userCollections: PHFetchResult<PHCollection>!
func photoLibraryDidChange(_ changeInstance: PHChange) {
// 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.
DispatchQueue.main.sync {
// Check each of the three top-level fetches for changes.
if let changeDetails = changeInstance.changeDetails(for: allPhotos) {
// Update the cached fetch result.
allPhotos = changeDetails.fetchResultAfterChanges
// (The table row for this one doesn't need updating, it always says "All Photos".)
}
// Update the cached fetch results, and reload the table sections to match.
if let changeDetails = changeInstance.changeDetails(for: smartAlbums) {
smartAlbums = changeDetails.fetchResultAfterChanges
tableView.reloadSections(IndexSet(integer: Section.smartAlbums.rawValue), with: .automatic)
}
if let changeDetails = changeInstance.changeDetails(for: userCollections) {
userCollections = changeDetails.fetchResultAfterChanges
tableView.reloadSections(IndexSet(integer: Section.userCollections.rawValue), with: .automatic)
}
}
}
关于ios - 照片库更改时快速观察者模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38763642/