2

目前,我有这段代码:

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 滚动浏览内容。将感谢任何帮助

4

1 回答 1

3

来自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)
        }

    }
}
于 2016-08-04T09:41:44.710 回答