CKAsset不会显示在tableview图像中 [英] CKAsset won't show in tableview image
问题描述
我在cloudkit DB中有一个可选图像(已检查过数据库,如果我在测试中添加了图像,则会出现图像)。我创建了一个类,将记录字段初始化为我在tableview中使用的变量。我也有自定义单元格。但图像不会显示在我的自定义tableview单元格中。我不知道在tableview图像中是否有可选图像导致问题,或者我的cloudkit中的代码/设置是否存在问题。
I have an optional image in the cloudkit DB(checked DB and the image is there in cases where I added it in my testing). I have created a class that initializes the record fields into variables I use in my tableview. I have a custom cell as well. But the image won't display in my custom tableview cell. I don't know if having an optional image in a tableview image is causing a problem or if there's an issue with my code/settings in cloudkit.
我非常感谢任何帮助,因为我已经被困了一个多星期而且网上几乎没有什么可以解决这个问题。
Any help is greatly appreciated, as I've been stuck for over a week and there's little to nothing online about this.
这是我的班级代码:
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代码:
Here's my tableview code:
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示例
Also I've seen a couple ways of pulling a CKAsset into a UIImage. The first way, is how I saw Apple do it in their CloudKitAtlas project. Although I'm not well versed in Obj-C, I think I followed it correctly - CloudKitAtlas Example
我也看到它使用NSData完成( contentOFFile :)和UIImage(数据:),如这篇帖子所示: SO CKAsset示例
I've also seen it done using NSData(contentOFFile:) and UIImage(data:), as shown in this post here: SO CKAsset Example
推荐答案
尝试不使用.paths并使用contentsOfURL。这就是我这样做的方法(在你的ifAs中为CKAsset):
Try doing it without the .paths and using contentsOfURL. This is how I do this (within your if for the CKAsset):
if var data = NSData(contentsOfURL: pic!.fileURL) {
self.postPic = UIImage(data: data!)!
}
除此之外......你做一个dequeueReusableCellWithIdentifier但是不检查是否它返回零。你应该创建一个新的单元格实例如果它是零
And besides that... You do a dequeueReusableCellWithIdentifier but don't check if it returns nil. You should create a new cell instance If it was nil
这篇关于CKAsset不会显示在tableview图像中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!