1
私は1000以上のレコード(画像資産でimage.jpg〜1.0メガバイト)とCloudKitデータベースを持っているロード1000の記録
私は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
}
あなたの質問は何ですか?イメージを表示するために 'UICollectionView'を使う必要があるのですか?または、実際には動作しない上記のコードに関する問題はありますか? – Fahim
私は恐怖です。私のアプリがクラッシュする可能性があります。セルは常に大きなデータをロードします。ですから、イメージをセルにロードするか、イメージを配列にロードして後でcollectionViewをリロードする必要がありますか? –
'UICollectionView'と' UITableView'の両方は、可視セルのデータだけを読み込みます。だから彼らは一度にすべてのデータをロードするつもりはありません。しかし、画像が大きければ、ユーザーがスクロールするときに、画像をダウンロードする必要がある場合には、多くの帯域幅を消費します。 – Fahim