首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swift中CKAsset加载的无序图像

Swift中CKAsset加载的无序图像
EN

Stack Overflow用户
提问于 2016-04-23 15:57:55
回答 2查看 343关注 0票数 0

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

下面是cellForRowAtIndexPath的代码:

代码语言:javascript
复制
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
}

提前感谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-04-23 16:39:58

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

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2016-04-24 17:04:29

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

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36813061

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档