CKAsset在表格视图中无法显示图片

4
我在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!)

    }
}

}

这是我的tableview代码:

    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项目中看到的方式。虽然我不太精通Obj-C,但我认为我已经正确理解了 - CloudKitAtlas示例

我还看到过使用NSData(contentOFFile:)和UIImage(data:)来完成此操作,如此处所示:SO CKAsset示例

2个回答

3
尝试不使用 .paths,改用 contentsOfURL。以下是我的做法(在 CKAsset if 中):
         if var data = NSData(contentsOfURL: pic!.fileURL) {
            self.postPic =  UIImage(data: data!)!
        }

此外...您使用了dequeueReusableCellWithIdentifier,但没有检查它是否返回nil。如果返回nil,您应该创建一个新的cell实例。


感谢您的回复。我在第一行(if pic!= nil)遇到了这个错误,但不确定它的含义 -“无法使用列表类型(@ | CKAsset,NIlLiteralConvertible)的参数调用!=”。 - Renee Olson
我尝试了以下代码,成功消除了错误,但图片仍未在tableview中显示。(并且我已将类变量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!)! } }` - Renee Olson
我在视图控制器的viewDidLoad方法中调用我的数据库查询函数。完成块包含reloadData方法。这样说对吗? - Renee Olson
我尝试将其放在viewDidAppear中,但没有帮助。此外,我在var postPic的初始化行以及cell.postPic.image上设置了断点,在这两种情况下,值都为nil。我一定是漏掉了什么。感谢您的帮助,我非常感激。 - Renee Olson
1
我在数据库查询代码中找到了错误!感谢您建议使用断点。这让我更深入地挖掘了一段时间以前编写的代码。在我的查询中,我使用了属性“desiredKeys”,但由于我还没有学会如何将图像保存到数据库中,所以忽略了图像字段。现在您的代码可以成功拉取图像了。对此我感到非常抱歉。很傻的是我没有仔细检查那段代码。再次感谢您! - Renee Olson
显示剩余5条评论

1
你可以像这样显示来自CKAsset的图像:
    var img = record.valueForKey("Picture") as? CKAsset
    cell.imageView?.image = UIImage(contentsOfFile: img!.fileURL.path!)

希望这有所帮助!

我的代码之前的确可以运行,但是我在数据库查询上搞砸了。如果数据库中没有可选图片,我相信你的代码也能正常工作。虽然有几种方法可以解决这个问题,但我认为我之前的代码最简洁。同时,Edwin的代码也可以在我的类的“if语句”中使用。感谢你的回复! - Renee Olson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接