Cha*_*ish 6 ios swift corespotlight alamofireimage
我正在尝试构建一个使用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")
}
}
})
Run Code Online (Sandbox Code Playgroud)
我要尝试做的下一件事是,我有一个名为的图像数组self._images
。该数组内部是一个空字符串(""
)或URL字符串("https://mywebsite.com/image.png"
)。
目前,我可以使用AlamofireImage使用以下代码将UIImageView的图像设置为该URL上的图像。
if (self._images[0] != "") {
myImageView.af_setImage(withURL: URL(string: self._images[0])!)
}
Run Code Online (Sandbox Code Playgroud)
如何将相同的图像放入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")
}
}
})
}
Run Code Online (Sandbox Code Playgroud)
已编辑
为什么不是好的解决方案?为了避免异步问题,我只是将所有内容都放在一个异步闭包中并进行同步图像下载。
更新
添加了默认的 Alamofire 兑现,在下载之前首先查看兑现的图像。
这是使用thumbnailURL 或thumbnailData 的更好解决方案,因为您有缓存的图像会抛出alamofire、SDWebImage ...
它简单而完美的性能明智
对于thumbnailURL,它只接受本地路径:
“文件://”。表明它是本地路径
if let path = SDImageCache.shared().defaultCachePath(forKey: item.url_thumbnail), let url = URL(string: "file://\(path)") {
attributeSet.thumbnailURL = url
}
Run Code Online (Sandbox Code Playgroud)
对于缩略图数据:
只需分配本地 url 路径
attributeSet.thumbnailData = try? Data(contentsOf: url)
Run Code Online (Sandbox Code Playgroud)
调试并简单地了解正在发生的事情
do {
attributeSet.thumbnailData = try Data(contentsOf: url)
}catch (let error as NSError){
print(error)
}
Run Code Online (Sandbox Code Playgroud)
请注意,我使用 SDWebImage 来获取缓存图像的路径,因为我使用它来缓存照片