1
我使用CoreSpolight框架將值分配給CSSearchableItem。我想優化圖像加載。CoreSpotlight索引 - 圖像加載
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不起作用。圖像未分配
當我嘗試在後臺下載它時。它不分配圖像到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
}
}