gpt4 book ai didi

ios - CKAsset 不会显示在表格 View 图像中

转载 作者:搜寻专家 更新时间:2023-10-31 08:34:13 26 4
gpt4 key购买 nike

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

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

这是我的类(class)代码:

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!)

}
}

这是我的表格 View 代码:

    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 的方法。第一种方式,是我看到 Apple 在他们的 CloudKitAtlas 项目中的做法。虽然我不太精通 Obj-C,但我认为我正确地遵循了它 - CloudKitAtlas Example

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

最佳答案

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

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

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

关于ios - CKAsset 不会显示在表格 View 图像中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28290142/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com