使用iOS 11 / Swift 4,我试图从一个图片库中过滤出一些瞬间。我只想要Places。使用nil选项检索moments集合,我得到了13个moments,其中8个moments的标题(location)为非空。当我使用一个简单的谓词localizedTitle != nil执行fetch时刻时,结果总是为零。用空的String ("")替换nil也会产生空的结果。
以下是示例代码和控制台结果:
let momentOptions = PHFetchOptions()
momentOptions.predicate = NSPredicate(format:"localizedTitle != nil")
momentList = PHAssetCollection.fetchMoments(with: nil)
let momentListFiltered = PHAssetCollection.fetchMoments(with: momentOptions)
let assetCount = momentList.count
for index in 0...assetCount-1 {
let a = momentList[index]
let sta = a.localizedTitle
let stb = a.localizedLocationNames
print(index, sta ?? "--", stb)
}控制台上的结果:
0 -- []
1 -- []
2 Point Reyes National Seashore ["Point Reyes Station, CA"]
3 Þingeyjarsveit, Northeast Iceland ["Goðafossvegur"]
4 Djúpavogshreppur ["East Iceland"]
5 Rangárþing eystra ["South Iceland"]
6 New York - Washington Heights ["W 168th St"]
7 -- []
8 -- []
9 Jacksonville, NC ["Western Blvd"]
10 -- []
11 Locust Shade Park ["Triangle, VA"]
12 Piedmont Triad International Airport ["Friendship, NC"]
(lldb) po momentListFiltered
<PHFetchResult: 0x60c0002e2100> count=0最后,为了确认正确的比较:
p momentList[7].localizedTitle == nil
(Bool) $R2 = true发布于 2017-12-09 03:45:36
这似乎是Photos API中的一个错误或未记录的限制。我鼓励你通过Bug Reporter向苹果提交一个bug。
您可能会发现此替代方法很有用:
let momentLists = PHCollectionList.fetchMomentLists(with: .momentListCluster, options: nil)
if momentLists.count > 0 {
for index in 0...momentLists.count - 1 {
let a = momentLists[index]
print(index, a.localizedTitle ?? "--", a.localizedLocationNames)
}
} else {
print("-- No moment lists! --")
}这将获得一个moment集群列表,该列表似乎可以按地点和时间对照片进行分组。
https://stackoverflow.com/questions/47637654
复制相似问题