我正在尝试构建一个测试应用程序,使用CoreSpotlight使内容在iOS Spotlight中可搜索。目前,每当用户点击刷新按钮时,它都会进行服务器请求,然后使用deleteAllSearchableItems
和indexSearchableItems
(可能不是最好的方法,但现在对我来说是最简单的,因为我仍然处于开始阶段)。
以下是我执行该操作的代码。
var searchableItems: [CSSearchableItem] = []
for i in 0 ..< self._ids.count {
let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeItem as String)
attributeSet.title = self._titles[i]
attributeSet.contentDescription = self._descriptions[i]
let searchableItem = CSSearchableItem(uniqueIdentifier: self._ids[i], domainIdentifier: "com.domain.appname", attributeSet: attributeSet)
searchableItems.append(searchableItem)
}
CSSearchableIndex.default().deleteAllSearchableItems(completionHandler: { (error) -> Void in
CSSearchableIndex.default().indexSearchableItems(searchableItems) { (error) -> Void in
if error != nil {
print(error?.localizedDescription ?? "Error")
}
}
})
接下来我要做的事情是使用self._images
这个图片数组。这个数组中有两种类型的元素:空字符串(""
)或者URL字符串("https://mywebsite.com/image.png"
)。
目前,我可以使用以下代码将UIImageView的图像设置为该URL处的图像,使用AlamofireImage库。
if (self._images[0] != "") {
myImageView.af_setImage(withURL: URL(string: self._images[0])!)
}
如何将同一张图片添加到CoreSpotlight attributeSet中?
我猜
attributeSet.thumbnailData.af_setImage(withURL: imagedownloadURL)
不起作用,尤其是因为它是异步的。最简单的集成方法是什么?
我尝试了一些东西,如for循环等,但最大的问题是它是异步的,而且我无法找到一个解决办法,因为它是异步的,而且我有多个项目。
非常感谢您的帮助!
af_setImage
,它似乎不会每次重新加载图像。只有第一次。 - Charlie Fish