来自 URL 的 iOS CoreSpotlight 缩略图

标签 ios swift alamofireimage corespotlight

我正在尝试构建一个测试应用程序,该应用程序使用 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/

相关文章:

ios - 在iOS中的UIView上连续按下手势

ios - 如何将电子邮件 ID 设置为 ABRecordRef 对象

xcode - 下标有问题

ios - 将图像大小限制为 ScrollView,Swift

ios - 如何让 UIButton 响应触摸 PNG 图像的透明区域?

swift - 动画标签以增加和减小尺寸

ios - 带有 UITableView 单元格的 Swift 4 AlamofireImage

ios - 快速滚动时,带有 AlamofireImage 的 Swift UItableview 崩溃

iOS)当我接近秒级别时,timeIntervalSince70 的值是固定的