- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试构建一个测试应用程序,该应用程序使用 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 循环等,但最大的问题是异步,我似乎无法找到解决方案,因为它是异步的,而且我有多个项目。
提前致谢!
最佳答案
最简单但不是最好的解决方案...
DispatchQueue.global(qos: .userInitiated).async {
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)
if let imageUrl = URL(string: self._images[i]) {
let urlRequest = URLRequest.init(url: imageUrl)
if let cashedImage = UIImageView.af_sharedImageDownloader.imageCache?.image(for: urlRequest, withIdentifier: nil) {
if let data = UIImageJPEGRepresentation(cashedImage, 1.0) {
attributeSet.thumbnailData = data
}
} else {
if let data = NSData.init(contentsOf: imageUrl) {
attributeSet.thumbnailData = data as Data
if let image = UIImage.init(data: data as Data) {
let urlRequest = URLRequest.init(url: imageUrl)
UIImageView.af_sharedImageDownloader.imageCache?.add(image, for: urlRequest, withIdentifier: nil)
}
}
}
}
searchableItems.append(searchableItem)
}
CSSearchableIndex.default().deleteAllSearchableItems(completionHandler: { (error) -> Void in
CSSearchableIndex.default().indexSearchableItems(searchableItems) { (error) -> Void in
if error != nil {
print(error?.localizedDescription ?? "Error")
}
}
})
}
已编辑
为什么不是好的解决方案?为了避免异步问题,我只是将所有内容都放在一个异步闭包中并进行同步图像下载。
更新
添加了默认的 Alamofire 兑现,在下载之前首先查看兑现的图像。
关于来自 URL 的 iOS CoreSpotlight 缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44103197/
文档提到: https://developer.apple.com/reference/corespotlight/cssearchableindex?language=objc In batch m
我想让我的应用程序为 Spotlight 索引做好准备,所以我得到了下面的代码来向 Core Spotlight 添加一个项目: CSSearchableItemAttributeSet *attri
在我的 IOS9 应用程序中,我可以使用 CoreSpotlight 库将数据添加到 Spotlight 的索引(即,当我使用 Spotlight 搜索框进行搜索时,我添加到索引的内容会显示出来)。
您好,我正在尝试在我的应用中实现 CoreSpotlight。 索引时我需要每次都运行这个还是在第一次安装应用程序时运行一次就足够了?如果应用被删除,我需要重新索引吗? 这是我使用的代码: - (vo
我正在使用 CoreSpotLight api 来索引一些内容。出于某种原因,我在搜索时找不到数据聚光灯。 let atset:CSSearchableItemAttributeSet = CSSea
我希望我的设备能够根据聚光灯人物索引预测谁在给我打电话。我已将人员信息上传到聚光灯索引,并且系统在我搜索时提供信息,但在有人打电话时则不提供信息。下面的代码做了所有这些事情,我不明白出了什么问题 if
在 WWDC session 中搜索 API 简介。他们显示带有调用按钮的 Airbnb 应用程序的搜索结果。据我所见,我认为结果是使用 CSSearchableItemAttributeSet 创建
我正在使用 CoreSpolight 框架为 CSSearchableItem 赋值。我想优化图片加载。 func setupSearchableContent(moviesInfo:NSMutabl
我正在尝试将聚光灯集成添加到我的 Mac 应用程序,但出现错误。步骤如下: 我创建了一个新的 cocoa 应用 我已将 CoreSpotlight 和 CoreServices 框架添加到项目buil
我正尝试在 Core Spotlight 的帮助下让我的应用内内容可搜索。一切正常,标题和描述都出现了,但没有缩略图。 这是我正在尝试的: CSSearchableItemAttributeSet *
CoreSpotlight 示例: https://applidium.com/en/news/ios9_search_api/ Spotlight 索引扩展示例: http://apprize.in
我花了很多时间来寻找 NSUserActivity 相对于 CoreSpotlight 的好处,但实际上我找不到任何东西。 实际上,它无法索引我们通过 NSUserActivity 设置的项。我在下面
我正在尝试构建一个测试应用程序,该应用程序使用 CoreSpotlight 使内容可在 iOS Spotlight 中搜索。目前,我将其设置为每当用户点击刷新按钮时,它都会发出服务器请求,然后 del
我正在使用“半静态”CoreSpotlight db 在需要时动态创建和替换 Spotlight Search 的概念证明。 这是我用于删除和重新创建整个内容的代码(假设 items 是 CSSear
如何在 CoreSpotlight 框架和 NSUserActivity 中选择最适合搜索编程的框架。 最佳答案 核心聚光灯 不要求用户访问内容以对其进行索引(在任何时候索引内容) 私有(privat
我有以下几行代码: var attributes = new CSSearchableItemAttributeSet (); attributes.Title = "Title"; attribut
是否可以使用iOS9的新特性如NSUserActivity和CoreSpotlight,但还是将我的Development Target设置为8.2,这样iOS8的用户仍然可以使用应用程序? 我假设我
是否有 corespotlight 搜索功能的示例代码 - iOS 9 API?如果可以查看示例代码来实现/测试,我将不胜感激。 最佳答案 创建一个新的 iOS 项目并将 CoreSpotlight
我正在努力将 CoreSpotlight 框架功能与应用程序集成,并尝试在 iOS 8.3 设备上运行它。每次我得到: dyld: Library not loaded: /System/Librar
我是一名优秀的程序员,十分优秀!