【问题标题】:CKAsset won't show in tableview imageCKAsset 不会显示在 tableview 图像中
【发布时间】:2015-02-03 01:58:46
【问题描述】:

我在 cloudkit 数据库中有一个可选图像(检查过的数据库,如果我在测试中添加它,图像就在那里)。我创建了一个类,它将记录字段初始化为我在表格视图中使用的变量。我也有一个自定义单元格。但图像不会显示在我的自定义表格视图单元格中。我不知道 tableview 图像中的可选图像是否会导致问题,或者我的 cloudkit 中的代码/设置是否存在问题。

非常感谢任何帮助,因为我已经被困了一个多星期,网上几乎没有关于此的内容。

这是我的课程代码:

var feedResults = [UserPosts]()

class UserPosts {

var record: CKRecord
var description: String
var username: String
//var image: CKAsset? // tried also initializing the CKAsset separately
var postPic: UIImage?


init(record: CKRecord) {

    self.record = record
    self.description = record.objectForKey("description") as String
    self.username = record.objectForKey("username") as String
   // self.image = record.objectForKey("postPic") as? CKAsset


    if var pic = record.objectForKey("postPic") as CKAsset! {

           self.postPic = UIImage(contentsOfFile: pic.fileURL.path!)

            // below is the other way I've seen it done.
               // var url = pic.fileURL!
               // var imageData = NSData(contentsOfFile: url.path!)
              // self.postPic = UIImage(data: imageData!)

    }
}

}

这是我的表格视图代码:

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as TableViewCell

    let record = feedResults[indexPath.row]
    cell.postDescription.text = record.description
    cell.userName.text = record.username

    if record.postPic != nil {

         cell.postPic.image = record.postPic!

    }

    return cell
}

我还看到了几种将 CKAsset 拉入 UIImage 的方法。第一种方式是我在他们的 CloudKitAtlas 项目中看到 Apple 的做法。虽然我对 Obj-C 不是很精通,但我认为我正确地遵循了它 - CloudKitAtlas Example

我还看到它使用 NSData(contentOFFile:) 和 UIImage(data:) 完成,如本文所示:SO CKAsset Example

【问题讨论】:

    标签: ios uitableview swift uiimage cloudkit


    【解决方案1】:

    尝试不使用 .paths 并使用 contentsOfURL。这就是我这样做的方式(在 CKAsset 的 if 范围内):

             if var data = NSData(contentsOfURL: pic!.fileURL) {
                self.postPic =  UIImage(data: data!)!
            }
    

    除此之外...您执行 dequeueReusableCellWithIdentifier 但不检查它是否返回 nil。你应该创建一个新的单元格实例如果它是 nil

    【讨论】:

    • 感谢您的回复。我在第一行收到了这个错误(如果 pic != nil),但不确定它的含义 - “不能调用!= 带有列表类型的参数(@|CKAsset,NIlLiteralConvertible)。
    • 我尝试了以下代码,它摆脱了错误,但图片仍然没有显示在 tableview 中。 (并且我将类 variable=image 初始化为 CKAsset):if record.objectForKey("postPic") != nil { self.image = record.objectForKey("postPic") as CKAsset! if image != nil { var data = NSData(contentsOfURL: image!.fileURL) if data != nil { self.postPic = UIImage(data: data!)! } }
    • 啊...对不起...没有注意到'if var pic..'结构。当然,您可以跳过“if pic != nil”检查。
    • 在您显示表格之前,是否还没有检索到 CKAsset?当返回带有资产的记录时,您可以在 tableView 上执行 .reloadData。
    • 我在数据库查询代码中发现了我的错误!感谢您建议断点。这让我更深入地研究了我不久前写的代码。在我的查询中,我使用了属性“desiredKeys”并省略了图像字段,因为我还没有学会如何将图像保存到数据库中。您的代码现在可以用于提取图像。我很抱歉这件事。感觉很傻,我没有先彻底检查该代码。再次非常感谢!
    【解决方案2】:

    您可以像这样显示来自 CKAsset 的图像:

        var img = record.valueForKey("Picture") as? CKAsset
        cell.imageView?.image = UIImage(contentsOfFile: img!.fileURL.path!)
    

    希望这会有所帮助!

    【讨论】:

    • 证明我上面的代码可以工作,但我的数据库查询搞砸了。如果我在数据库中没有可选图像,我相信您的代码会起作用。有几种方法我可以做到,但我认为我拥有上述代码的方式是最简洁的。 Edwin 的代码也适用于我的类“if 语句”。感谢您的回复!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-19
    • 2012-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多