gpt4 book ai didi

ios - 照片库更改时的 Swift 观察者模式

转载 作者:可可西里 更新时间:2023-11-01 00:58:56 25 4
gpt4 key购买 nike

目前,我有那段代码:

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 - 照片库更改时的 Swift 观察者模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38763642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com