【发布时间】: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