来自CKAsset的图像在Swift中加载乱序

时间:2016-04-23 15:57:56

标签: ios swift uitableview cloudkit ckasset

我正在加载从公共CloudKit数据库中获取的图像的tableview作为CKAssets。但是,图像按顺序加载大约两秒钟,直到将正确的图像加载到自定义UITableview单元格的UIImageView中。我知道问题在于,由于单元格是可重复使用的,因此当用户在图像视图中显示正确的图像之前滚动浏览TableView时,仍然会从CloudKit下载图像并显示在任何可见单元格中。我想知道是否在swift中有一个修复程序,以便下载的图像仅用于可见单元格而不是任何以前的单元格。

以下是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
}

提前致谢!

2 个答案:

答案 0 :(得分: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
                        }
                    }
                }

答案 1 :(得分:0)

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

How to determine when all images have been downloaded from a set in Swift?

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