Ios 将1000条带图像的记录加载到采集视图CloudKit Swift
我有一个超过1000条记录的CloudKit数据库(image Asset中的image.jpg~1.0 MB) 我应该在CollectionViewCell中加载图像吗 我的代码是:Ios 将1000条带图像的记录加载到采集视图CloudKit Swift,ios,swift,xcode,cloudkit,Ios,Swift,Xcode,Cloudkit,我有一个超过1000条记录的CloudKit数据库(image Asset中的image.jpg~1.0 MB) 我应该在CollectionViewCell中加载图像吗 我的代码是: func loadData(){ self.loading.startAnimating() let query = CKQuery(recordType: self.recordType, predicate: predicate) query.sortDescr
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)
}
My 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
}
您应该在用户每次在UICollectionView上滚动时提取一小组记录,而不是一次提取1000条记录 使用此选项,您可以获得更好的性能,并仅恢复所需的映像 为此,必须将
resultsLimit
属性设置为与显示CollectionView的图像数量相匹配的数字加上5或6
每次用户到达CollectionView的末尾时,您都应该执行另一次提取,并将CKQueryCursor作为参数传递。您的问题是什么?是否应该使用
UICollectionView
来显示图像?或者你有任何问题与上述代码实际上不工作?我担心,超过1.0 GB的加载可能是我的应用程序将崩溃。单元格总是加载大数据。那么,我应该将图像加载到单元格中还是将图像加载到数组中,稍后再重新加载collectionView呢?UICollectionView
和UITableView
都只加载可见单元格的数据。因此,他们不会一次加载所有数据。但是如果你的图像很大,那么当用户滚动时,它们会消耗大量带宽——如果必须下载图像的话。