照片库更改时的Swift观察者模式(再次)

时间:2018-06-02 08:00:38

标签: swift

正如本thread已经注意到的那样 photo change observer apple example运行正常。不过,以下代码没有。

初始化PHAssetCollection数组以仅显示smartAbums子集

var smartAlbums = [PHAssetCollection]()
let smartAlbumSubTypes = [
    PHAssetCollectionSubtype.smartAlbumUserLibrary,     // Camera Roll
    PHAssetCollectionSubtype.smartAlbumFavorites,       // Favorites
    PHAssetCollectionSubtype.smartAlbumSelfPortraits,   // Selfies
    PHAssetCollectionSubtype.smartAlbumPanoramas,       // Panormas
    PHAssetCollectionSubtype.smartAlbumScreenshots      // ScreenShot
]

for s in smartAlbumSubTypes {
        let smartAssetCollection = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: s, options: nil)
        if let smartCollection = smartAssetCollection.firstObject {
            if ( smartCollection.imageCount > 0 ) {
                smartAlbums.append(smartCollection)
            }
        }
    }

实施观察员

func photoLibraryDidChange(_ changeInstance: PHChange) {
    let photos = PHPhotoLibrary.authorizationStatus()
    if photos != .authorized {
        return
    }
    for s in albums.smartAlbums {
        if (changeInstance.changeDetails(for: s ) != nil) {
            DispatchQueue.main.sync {
                let indexPath = IndexPath(item: albums.smartAlbums.index(of: s)!, section: Section.smartAlbums.rawValue)
                albumCollectionView.reloadItems(at: [indexPath])
            }
        }
    }
}

观察者捕捉照片变化,但

if (changeInstance.changeDetails(for: s ) != nil)

返回零。有什么帮助吗?

0 个答案:

没有答案