为了在UITable中显示,我需要创建一个经过过滤的smartAlbums集合。我以前尝试过各种方法,将未过滤的集合转换为可变形式,并删除我希望排除的集合,然后重新转换回PHFetchResult。所有这些尝试都失败了。
我现在正在尝试使用PHFetch选项来过滤带有"localizedTitle“键的专辑,这是特别允许的(https://developer.apple.com/documentation/photos/phfetchoptions)。然而,当我尝试下面的测试用例代码来尝试排除"Videos“智能文件夹时,我在newAlbums中得到了零计数结果。当我将谓词设置为%K == %@时,我也得到了零结果。正确答案应该是前者为15,后者为1。为什么我的谓词没有选择正确的结果?我不希望将新的集合保存回库中,只是将其用于临时显示,所以我没有尝试请求调用(可能我误解了这里的框架?)
我已经搜索了S/O和developer.apple,工作代码的唯一示例是用于过滤单个媒体(照片或视频),而不是用于选择smartAlbums。
let fetchOptions = PHFetchOptions()
let p1 = NSPredicate(format: "%K == %@", "localizedTitle", "Videos")
fetchOptions.predicate = p1
let newAlbums = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .albumRegular, options: fetchOptions)发布于 2017-12-02 05:19:52
这是我发现的一种方法,我已经验证过它是有效的。非常感谢Michael https://stackoverflow.com/a/46145638/4945371
首先添加这个小方法,它返回的不是PHCollectionList,而是数组!
private func fetchSmartCollections(with: PHAssetCollectionType, subtypes: [PHAssetCollectionSubtype]) -> [PHAssetCollection] {
var collections:[PHAssetCollection] = []
let options = PHFetchOptions()
options.includeHiddenAssets = false
for subtype in subtypes {
if let collection = PHAssetCollection.fetchAssetCollections(with: with, subtype: subtype, options: options).firstObject {
collections.append(collection)
}
}
return collections
}然后创建所需的智能相册数组:
let subtypes:[PHAssetCollectionSubtype] = [
.smartAlbumFavorites,
.smartAlbumLongExposures,
.smartAlbumSelfPortraits,
.smartAlbumRecentlyAdded
// .smartAlbumUserLibrary,
// .smartAlbumPanoramas,
// .smartAlbumLivePhotos,
// .smartAlbumBursts,
// .smartAlbumDepthEffect,
// .smartAlbumScreenshots,
]最后,
smartAlbumsArray = fetchSmartCollections(with: .smartAlbum, subtypes: subtypes)而不是某些变体:
smartAlbums = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .albumRegular, options: smartAlbumOptions)https://stackoverflow.com/questions/46960105
复制相似问题