cloudkit DB中有一个可选的映像(选中了DB,在测试中添加了该映像)。我已经创建了一个类,它将记录字段初始化为我在表视图中使用的变量。我也有一个定制的牢房。但是图像不会显示在我的自定义的tableview单元格中。我不知道在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项目中看到它的。虽然我对Obj不太熟悉,但我认为我的做法是正确的-- CloudKitAtlas实例
我还看到了使用NSData(contentOFFile:)和UIImage(data:)完成的工作,如下文所示:所以CKAsset例子
发布于 2015-02-03 08:17:19
尝试不使用.paths并使用contentsOfURL来完成它。我就是这样做的(在您的if中用于CKAsset):
if var data = NSData(contentsOfURL: pic!.fileURL) {
self.postPic = UIImage(data: data!)!
}除此之外..。您可以执行一个dequeueReusableCellWithIdentifier,但不要检查它是否返回零。如果新单元格实例为0,则应创建该实例。
发布于 2015-02-12 02:10:19
您可以这样显示来自CKAsset的图像:
var img = record.valueForKey("Picture") as? CKAsset
cell.imageView?.image = UIImage(contentsOfFile: img!.fileURL.path!)希望这能有所帮助!
https://stackoverflow.com/questions/28290142
复制相似问题