我正在构建一个应用程序,它使用CoreData数据库将其数据存储在产品上。这些产品显示在UICollectionView中。此CollectionView中的每个单元格都显示其包含的产品的基本信息,包括图像。从CoreData在Swift中加载异步映像
虽然单元格相对较小,但它们显示的原始图像最好安静得很大,因为它们也应该能够以较大的图像视图显示。 > UICollectionViewCell { 让细胞= collectionView.dequeueReusableCell(withReuseIdentifier: “小区”,为:indexPath -
FUNC的CollectionView(IndexPath _的CollectionView:::UICollectionView,cellForItemAt indexPath)的图像在我的CellForItemAtIndexPath:
方法从CoreData直接装载)as! CollectionWineCell
var current: Product!
//product details are set on labels
//image is set
if current.value(forKey: "image") != nil {
let image = current.value(forKey: "image") as! WineImage
let loadedImage = UIImage(data: image.image)
cell.imageview.image = loadedImage
} else {
cell.imageview.image = UIImage(named: "ProductPlaceholder.png")
}
return cell
}
当产品集合增长时,滚动变得更加颠簸,大量帧被丢弃。这对我来说很有意义,但到目前为止我还没有找到合适的解决方案。在线查看时,大量文档和框架可用于从URL(联机或文件路径)加载异步映像,但从CoreData执行此操作看起来并不常见。
我已经尝试使用异步读取请求做:
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName:"ProductImage")
fetchRequest.predicate = NSPredicate(format: "product = %@", current)
let asyncRequest = NSAsynchronousFetchRequest(fetchRequest: fetchRequest) { results in
if let results = results.finalResult {
let result = results[0] as! ProductImage
let loadedImage = UIImage(data: result.image)
DispatchQueue.main.async(execute: {
cell.wineImage.image = loadedImage
})
}
}
_ = try! managedObjectContext.executeRequest(asyncRequest)
然而,这种方法似乎并不平滑的东西出来要么
问题 在显示大型数据集,包括来自CoreData的图像,如何在UICollectionView中以不会导致滞后和丢帧的方式加载图像?
谢谢你的回应!我将在我的申请中执行此操作,最迟会在明天回复给您! – Joris416
我已经实现了你在我的项目中所说的内容。我的图像在创建和/或接收时也会生成较小的缩略图,并且这些图像将异步加载到采集视图的图像中(使用NUKE),从而使滚动更加平滑。你的回答帮助了我可以继续我的项目而不用担心这一点,我感谢你。 – Joris416