我在 cloudkit DB 中有一个可选图像(已检查数据库,并且在我在测试中添加它的情况下该图像就在那里)。我创建了一个类,它将记录字段初始化为我在表视图中使用的变量。我也有一个定制单元。但图像不会显示在我的自定义表格视图单元格中。我不知道 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 的方法。第一种方法是我在 Apple CloudKitAtlas 项目中看到的方法。虽然我不太熟悉 Obj-C,但我认为我遵循的是正确的 -CloudKitAtlas 示例 https://developer.apple.com/library/ios/samplecode/CloudAtlas/Listings/CloudKitAtlas_AAPLCKAssetViewController_m.html#//apple_ref/doc/uid/TP40014599-CloudKitAtlas_AAPLCKAssetViewController_m-DontLinkElementID_6
我还看到它使用 NSData(contentOFFile:) 和 UIImage(data:) 完成,如本文中所示:SO CKAsset 示例 https://stackoverflow.com/questions/28206955/fetch-record-assets-in-cloudkit-using-swift
尝试不使用 .paths 并使用contentsOfURL。这就是我执行此操作的方法(在 CKAsset 的 if 内):
if var data = NSData(contentsOfURL: pic!.fileURL) {
self.postPic = UIImage(data: data!)!
}
除此之外...您执行 dequeueReusableCellWithIdentifier 但不检查它是否返回 nil。如果它为零,您应该创建一个新的单元实例
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)