ios - 照片库更改时快速观察者模式

标签 ios swift camera-roll

目前,我有这段代码:

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 滚动内容。将不胜感激任何帮助

最佳答案

来自https://developer.apple.com/library/prerelease/content/samplecode/UsingPhotosFramework/Listings/Shared_MasterViewController_swift.html#//apple_ref/doc/uid/TP40014575-Shared_MasterViewController_swift-DontLinkElementID_9

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/

相关文章:

ios - 当设备旋转处于 .PortraitUpsideDown 时,我永远不会收到通知

javascript - React Native 将图像保存到特定位置

ios - 使用特定文件名将照片保存到相机胶卷

ios - 办理电话: links in voip app

iphone - 如何在 IBOutletCollection 中隐藏一堆标签?

swift - 如何将模块导入到xcode中

ios - 按下按钮后如何显示 24 小时计数器

download - 将视频保存到 CameraRoll React-Native

ios - Alamofire 清除所有 cookie

ios - NSFetchedResultsController 没有收到新对象