gpt4 book ai didi

ios - CoreSpotlight 索引 - 图像加载

转载 作者:行者123 更新时间:2023-11-28 06:48:11 24 4
gpt4 key购买 nike

我正在使用 CoreSpolight 框架为 CSSearchableItem 赋值。我想优化图片加载。

func setupSearchableContent(moviesInfo:NSMutableArray)
{
if #available(iOS 9.0, *)
{
var searchableItems = [CSSearchableItem]()

for i in 0...(moviesInfo.count - 1)
{

let searchableItemAttributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeText as String)

// Set the title
searchableItemAttributeSet.title = (moviesInfo[i] as! ICFAssetObject).title!

// Movie Image
if let tempPosterImage = (moviesInfo[i] as! ICFAssetObject).portraitImage()
{
searchableItemAttributeSet.thumbnailData = getImageFromURL(tempPosterImage) //imageData
}

// Set the Description
searchableItemAttributeSet.contentDescription = (moviesInfo[i] as! ICFAssetObject).description!

//Set the Link and Slug
let values = NSString(format: "%@||%@", (moviesInfo[i] as! ICFAssetObject).link!,(moviesInfo[i] as! ICFAssetObject).slug!)

let searchableItem = CSSearchableItem(uniqueIdentifier:values as String ,domainIdentifier: "taimur_assets", attributeSet: searchableItemAttributeSet)

searchableItem.expirationDate = NSDate.distantFuture()
searchableItems.append(searchableItem)

CSSearchableIndex.defaultSearchableIndex().indexSearchableItems([searchableItem])
{
(error: NSError?) -> Void in

if let error = error
{
print("Indexing error: \(error.localizedDescription)")
}
else
{
print("Search item successfully indexed!")
}
}
}
}
else
{
// Fallback on earlier versions
}
}
func getImageFromURL(var fileURL:String) -> NSData
{
fileURL = NSString(format: "%@/180x180", fileURL) as String
let data:NSData = NSData(contentsOfURL: NSURL(string: fileURL)!)!
return data
}

下载图像时需要花费大量时间并且应用程序会挂起。

thumbnailURL doesn't work. Images are not assigned

当我尝试在后台下载它时。它不会将图像分配给 CSSearchableIndex 并且图像不会显示

let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0))
{
let data:NSData = self.getImageFromURL(movie.Image) //imageData
dispatch_async(dispatch_get_main_queue())
{
searchableItemAttributeSet.thumbnailData = data
}
}

最佳答案

我们来了!问题已修复

let priority = DISPATCH_QUEUE_PRIORITY_BACKGROUND
dispatch_async(dispatch_get_global_queue(priority, 0))
{
let data:NSData = self.getImageFromURL(movie.Image) //imageData
dispatch_async(dispatch_get_main_queue())
{
searchableItemAttributeSet.thumbnailData = data
}
}

关于ios - CoreSpotlight 索引 - 图像加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35721329/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com