0

我正在加载从公共 CloudKit 数据库作为 CKAssets 获取的图像的表格视图。但是,在将正确的图像加载到自定义 UITableview 单元格的 UIImageView 之前,图像会乱序加载大约两秒钟。我知道问题在于,由于单元格是可重复使用的,因此图像仍然从 CloudKit 下载并显示在任何可见的单元格中,而用户在正确的图像显示在图像视图中之前滚动 TableView 时。我想知道是否有快速解决此问题的方法,以便下载的图像仅适用于可见单元格的图像,而不适用于任何以前的单元格。

这是 cellForRowAtIndexPath 的代码:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! PostsTableViewCell
    cell.userInteractionEnabled = false

    photoRecord = sharedRecords.fetchedRecords[indexPath.row]

    cell.photoTitle.text = photoRecord.objectForKey("photoTitle") as? String

    cell.photoImage.backgroundColor = UIColor.blackColor()
    cell.photoImage.image = UIImage(named: "stock_image.png")

    if let imageFileURL = imageCache.objectForKey(self.photoRecord.recordID) as? NSURL {
        cell.photoImage.image = UIImage(data: NSData(contentsOfURL: imageFileURL)!)
        cell.userInteractionEnabled = true
        print("Image Cached: \(indexPath.row)")
    } else {

        let container = CKContainer.defaultContainer()
        let publicDatabase = container.publicCloudDatabase
        let fetchRecordsImageOperation = CKFetchRecordsOperation(recordIDs:[self.photoRecord.recordID])
        fetchRecordsImageOperation.desiredKeys = ["photoImage"]
        fetchRecordsImageOperation.queuePriority = .VeryHigh

        fetchRecordsImageOperation.perRecordCompletionBlock = {(record:CKRecord?, recordID:CKRecordID?, error:NSError?) -> Void in
            if let imageRecord = record {
                NSOperationQueue.mainQueue().addOperationWithBlock() {
                    if let imageAsset = imageRecord.objectForKey("photoImage") as? CKAsset{
                        cell.photoImage.image = UIImage(data: NSData(contentsOfURL: imageAsset.fileURL)!)
                        self.imageCache.setObject(imageAsset.fileURL, forKey:self.photoRecord.recordID)
                        cell.userInteractionEnabled = true
                        }
                    }
                }
            }
        publicDatabase.addOperation(fetchRecordsImageOperation)
        }
    return cell
}

提前致谢!

4

2 回答 2

1

fetchRecordsImageOperation.perRecordCompletionBlock在表格视图出现和被调用之间存在延迟。在这段时间内,用户可能会滚动表格视图,导致表格视图单元格出列并使用不同的 indexPath 和与之关联的不同数据重新排队,如果您不检查单元格的索引路径是否与您构造时相同fetchRecordsImageOperation.perRecordCompletionBlock,则此行:cell.photoImage.image = UIImage(data: NSData(contentsOfURL: imageAsset.fileURL)!)将导致图像放置在已经显示不同数据的单元格中。您可以像这样修改完成块以避免这种情况。

if let imageRecord = record {
                NSOperationQueue.mainQueue().addOperationWithBlock() {
                    if let imageAsset = imageRecord.objectForKey("photoImage") as? CKAsset{
                        if indexPath == tableView.indexPathForCell(cell){
                            cell.photoImage.image = UIImage(data: NSData(contentsOfURL: imageAsset.fileURL)!)
                        }
                        self.imageCache.setObject(imageAsset.fileURL, forKey:self.photoRecord.recordID)
                        cell.userInteractionEnabled = true
                        }
                    }
                }
于 2016-04-23T16:39:58.243 回答
0

我相信你在这里找到答案,我当然有偏见,因为我写了它。

如何确定何时从 Swift 中的集合中下载了所有图像?

您应该在加载图像时设置要显示的图像并显示它以便用户了解正在发生的事情?

于 2016-04-24T17:04:29.723 回答