我正在尝试构建一个测试应用程序,它使用CoreSpotlight使内容可以在iOS聚光灯中搜索。目前,我已经设置了它,每当用户点击刷新按钮时,它就会发出服务器请求,然后是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循环之类的,但最大的问题是异步,而且我似乎无法找到解决方案来完成这个任务,因为它是异步的,而且我有多个项。
提前谢谢你!
发布于 2018-02-06 07:59:50
这里有一个更好的解决方案使用thumbnailURL或thumbnailData,因为您有一个缓存的图像抛出轮毂,SDWebImage。
这是简单而完美的性能
For thumbnailURL它只接受本地路径:
"file://".指示它的本地路径
if let path = SDImageCache.shared().defaultCachePath(forKey: item.url_thumbnail), let url = URL(string: "file://\(path)") {
attributeSet.thumbnailURL = url
}For thumbnailData
只需分配本地url路径
attributeSet.thumbnailData = try? Data(contentsOf: url)调试和了解所发生的事情
do {
attributeSet.thumbnailData = try Data(contentsOf: url)
}catch (let error as NSError){
print(error)
}请注意,我正在使用SDWebImage获取缓存图像的路径,因为我使用它来缓存照片
https://stackoverflow.com/questions/44103197
复制相似问题