我有一个包含超过 1000 条记录的 CloudKit 数据库(image.jpg ~ 1.0 MB in image Asset)
我应该在 CollectionViewCell 中加载图像吗?
我的代码是:
func loadData(){
self.loading.startAnimating()
let query = CKQuery(recordType: self.recordType, predicate: predicate)
query.sortDescriptors = [NSSortDescriptor(key: "productID", ascending: true)]
operation.query = query
operation.resultsLimit = 1000
operation.qualityOfService = .userInteractive
operation.recordFetchedBlock = { (record) in
self.items.append(record)
DispatchQueue.main.async {
self.collectionView.reloadData()
}
}
operation.queryCompletionBlock = { (queryCursor, error) in
DispatchQueue.main.async {
self.loading.stopAnimating()
}
}
myData.add(operation)
}
我的 CollectionViewCell:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "productCell", for: indexPath)
let img = UIImageView(frame: CGRect(x: 1, y: 1, width: w - 2, height: h - 2))
img.contentMode = .scaleAspectFill
img.image = UIImage(contentsOfFile: (self.items[indexPath.item]["productImage"] as! CKAsset).fileURL.path)
cell.contentView.addSubview(img)
return cell
}
最佳答案
与其一次获取 1000 条记录,不如每次用户在您的 UICollectionView 上滚动时获取一小部分记录。
这样您可以获得更好的性能并仅恢复您需要的图像。
为此,您必须将 resultsLimit
属性设置为适合显示 CollectionView 的图像数量的数字加上 5 或 6
每次用户到达您的 CollectionView 的末尾时,您应该执行另一次获取,将 CKQueryCursor 作为参数传递。
关于ios - 使用 Image 加载 1000 条记录到 Collection View CloudKit Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43059976/