阅读背景:

使用Image将1000条记录加载到集合视图CloudKit Swift

来源:互联网 

I have a CloudKit database with over 1000 record (image.jpg ~ 1.0 MB in image Asset)

我有一个超过1000条记录的CloudKit数据库(图像资产中的image.jpg~1.0 MB)

Should I load Image in CollectionViewCell?

我应该在CollectionViewCell中加载Image吗?

My code is:

我的代码是:

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:

我的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
    }

1 个解决方案

#1


2  

Instead of fetch 1000 records one time you should fetch a small set each time user scrolls on your UICollectionView.

每次用户在UICollectionView上滚动时,您应该获取一个小集,而不是一次获取1000条记录。

With this you obtain better performance and recovers only the images that you need.

通过这种方式,您可以获得更好的性能并仅恢复所需的图像。

To do this you must set the resultsLimit property to a number that fits with images quantity that shows your CollectionView plus 5 or 6

为此,您必须将resultsLimit属性设置为适合显示CollectionView加5或6的图像数量的数字

Each time user reach the end of your CollectionView you should perform another fetch passing the CKQueryCursor as a parameter.

每次用户到达CollectionView的末尾时,您都应该执行另一次提取,并将CKQueryCursor作为参数传递。


分享到: