CoreSpotlight 索引 - 图像加载
CoreSpotlight Indexing - Image Loading
我正在使用 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
}
}
我正在使用 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
}
}