过滤PHAssetCollection中的时刻

时间:2017-12-04 16:21:12

标签: ios swift photokit

使用iOS 11 / Swift 4,我试图从照片库中过滤掉一系列瞬间。我只想要@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_graph_test, container, false); BarChart chart = view.findViewById(R.id.bar_Chart_test); List<BarEntry> entries = new ArrayList<>(); entries.add(new BarEntry(0f, 30f)); entries.add(new BarEntry(1f, 80f)); entries.add(new BarEntry(2f, 60f)); entries.add(new BarEntry(3f, 50f)); // gap of 2f entries.add(new BarEntry(5f, 70f)); entries.add(new BarEntry(6f, 60f)); BarDataSet set = new BarDataSet(entries, "BarDataSet"); BarData data = new BarData(set); data.setBarWidth(0.9f); // set custom bar width chart.setData(data); chart.setFitBars(true); // make the x-axis fit exactly all bars chart.invalidate(); // refresh return view; } 。使用Places选项检索一系列时刻,我得到13个时刻,其中8个时刻具有非零标题(位置)。当我使用简单谓词nil执行获取时刻时,结果始终为零。将空localizedTitle != nil替换为nil也会产生空结果。

以下是示例代码和控制台结果:

String ("")

控制台上的结果:

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

1 个答案:

答案 0 :(得分:1)

这似乎是照片API中的错误或未记录的限制。我鼓励您通过Bug Reporter向Apple提交错误。

您可能会发现这种替代方法很有用:

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! --")
}

这会得到一个时刻聚类列表,它似乎按地点和时间对照片进行分组。